User Tools

Site Tools


c:stdio.h:fgets

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

c:stdio.h:fgets [2013/01/22 22:02] (current)
Line 1: Line 1:
 +{{keywords>wiki library source code example reference}}
 +===== fgets =====
 +
 +<code c>
 +    char *fgets(char *str, int n, FILE *stream);
 +</code>
 +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.
 +
 +===== C Sourcecode Example =====
 +<code c>
 +/* 
 + * 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;
 +}
 +
 +</code>
 +=== test.txt ===
 +content of test.txt
 +<code c>
 +test 123456789,123456789
 + ... # ...
 +this is a test for code-reference.com 
 +</code>
 +
 +==== fgets output example ====
 +    output:./fgets 
 +    test 123456789,1234
 +
 +
  
advertising
c/stdio.h/fgets.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz