How to use the fgets() function in C

PHOTO EMBED

Sun Oct 04 2020 21:07:26 GMT+0000 (Coordinated Universal Time)

Saved by @BananaFudgkins #c

#include <stdio.h>

int main(){
  char str[20];
  
  fgets(str, 20, stdin); // read from stdin
  puts(str); // print read content out to stdout
  
  // open the file
  FILE *f = fopen("file.txt" , "r"); 
  
  // if there was an error
  if(f == NULL){
    perror("Error opening file"); // print error
    return(-1); 
  }
  // if there was no error
  else{ 
    fgets(str, 20, f); // read from file
    puts(str); // print read content out to stdout
  }
  
  fclose(f); // close file
  
  return(0);
}
content_copyCOPY

Uses the fgets() function to read an entire line from a file

https://www.educative.io/edpresso/how-to-use-the-fgets-function-in-c