User Tools

Site Tools


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.

C Sourcecode Example

 * fgets example code
#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);
  return 0;


content of test.txt

test 123456789,123456789
 ... # ...
this is a test for 

fgets output example

  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