#include <stdio.h>
|
char *
fgets (char * restrict str, int size, FILE * restrict stream); |
char *
gets (char *str); |
The gets function is equivalent to fgets with an infinite size and a stream of stdin, except that the newline character (if any) is not stored in the string. It is the callers responsibility to ensure that the input line, if any, is sufficiently short to fit in the string.
/****************** this program shows reading characters from a file using fgets **************/ /****************** consider input.txt has the following content: ******************************/ /****************** abcdefghijklmn **********************************************************************/ /****************** fdsfdsafsdabcdefghijklmn*************************************************************/ #include <stdio.h> int main(void) { char buf[20]; FILE* fp = fopen("c:\nput.txt", "w"); fprintf(fp, "%s", "abcdefghijklmn"); fprintf(fp, "%c", "\n"); fprintf(fp, "%s", "fdsfdsafsdabcdefghijklmn"); fclose(fp); fp = fopen("C:\nput.txt","r"); if(fp == NULL) { printf("fopen failed\n"); return -1; } if(fgets(buf,18,fp) != NULL) printf("%s", buf); else printf("Buffer is empty\n"); buf[0] = '\0'; if(fgets(buf,2,fp) != NULL) printf("%s\n", buf); else printf("Buffer is empty\n"); fclose(fp); return 0; }
Output
abcdefghijklmn fdsfdsafsdabcdefghijklmn
[EBADF] | |
The given stream is not a readable stream. | |
The function fgets may also fail and set errno for any of the errors specified for the routines fflush, fstat, read, or malloc.
The function gets may also fail and set errno for any of the errors specified for the routine getchar.
Feedback
For additional information or queries on this page send feedback
© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user. |