1 #include <stdio.h> 2 #include <stdlib.h> //for EXIT_FAILURE,exit() 3 4 int main(void) 5 { 6 FILE *fp; 7 char arr[100]; 8 9 // write something to test file 10 fp = fopen("test","w"); 11 if(fp == NULL) 12 { 13 puts("File open failed."); 14 exit(EXIT_FAILURE); 15 } 16 fprintf(fp,"%s","nihaoa"); 17 fclose(fp); 18 19 //output the file,exactually just one line 20 fp = fopen("test","r"); 21 if(fp == NULL) 22 { 23 puts("Cannot open the file for reading."); 24 exit(EXIT_FAILURE); 25 } 26 if(fgets(arr,100,fp) != NULL) 27 puts(arr); 28 fclose(fp); 29 30 return 0; 31 }