1 // 2 // Created by jia on 06/01/19. 3 // 4 #include "../header.h" 5 const char* path; 6 const char* path_0 = "/home/jia/Documents/"; 7 const char* file_name; 8 const char* content; 9 10 void init_file(){ 11 path = malloc(sizeof(char) * BUFF); 12 strcat(path, path_0); 13 file_name = malloc(sizeof(char) * 20); 14 printf("Please input your file name:"); 15 scanf("%s", file_name); 16 strcat(path, file_name); 17 content = malloc(sizeof(char) * BUFF); 18 } 19 20 21 void create_file(){ 22 23 init_file(); 24 25 FILE* create_file; 26 27 create_file = fopen(path, "w"); 28 if(create_file == NULL){ 29 printf("ERROR"); 30 exit(1); 31 }else{ 32 content = "Hello, my name is Jia"; 33 fprintf(create_file, "%s", content); 34 printf("\nFile had been created please check the path: %s", path); 35 fclose(create_file); 36 } 37 } 38 void read_file(){ 39 init_file(); 40 41 FILE* readfile = fopen(path, "r"); 42 if(readfile == NULL){ 43 printf("ERROR"); 44 exit(1); 45 }else{ 46 //read all the lines 47 /*char last; 48 do{ 49 last = fgetc(readfile); 50 printf("%c", last); 51 }while(last != EOF);*/ 52 53 //read only one line 54 fscanf(readfile, "%[^\n]", content); 55 printf("%s\n", content); 56 fclose(readfile); 57 58 } 59 }