Programming Reference/Librarys
Question & Answer
Q&A is closed
char *fgets(char *str, int n, FILE *stream);
Copies characters from (input) stream stream to sstr, stopping when n-1 characters copied, newline copied, end-of-file reached or error occurs. If no error, str is NUL-terminated. Returns NULL on end-of-file or error, str otherwise.
/* * fgets example code * http://code-reference.com/c/stdio.h/fgets */ #include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ #define MAX 20 int main( void ) { FILE *stream; char string[MAX]; if((stream=fopen("test.txt","r"))==NULL) { printf("Cannot open file.\n"); return 1; } fgets(string, MAX, stream); printf("%s\n", string); fclose(stream); return 0; }
content of test.txt
test 123456789,123456789 ... # ... this is a test for code-reference.com
output:./fgets test 123456789,1234