Advertisement
kamandos

HomeWork

Jul 15th, 2022
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.   char str[20];
  5.  
  6.   fgets(str, 20, stdin); // read from stdin
  7.   puts(str); // print read content out to stdout
  8.  
  9.   // open the file
  10.   FILE *f = fopen("file.txt" , "r");
  11.  
  12.   // if there was an error
  13.   if(f == NULL){
  14.     perror("Error opening file"); // print error
  15.     return(-1);
  16.   }
  17.   // if there was no error
  18.   else{
  19.     fgets(str, 20, f); // read from file
  20.     puts(str); // print read content out to stdout
  21.   }
  22.  
  23.   fclose(f); // close file
  24.  
  25.   return(0);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement