User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:stdio.h:fgets

fgets

    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.

C Sourcecode Example

/* 
 * 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;
}

test.txt

content of test.txt

test 123456789,123456789
 ... # ...
this is a test for code-reference.com 

fgets output example

  output:./fgets 
  test 123456789,1234

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/stdio.h/fgets.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz