1. 程式人生 > >c programming create a file

c programming create a file

 1 void create_file(){
 2     char* path = malloc(sizeof(char) * BUFF);
 3     char* path_0 = "/home/jia/Documents/";
 4     strcat(path, path_0);
 5     char* file_name = malloc(sizeof(char) * 20);
 6     printf("Please input your file name:");
 7     scanf("%s", file_name);
 8     strcat(path, file_name);
9 char* content = "hello, I can create a new file"; 10 FILE* create_file; 11 12 create_file = fopen(path, "w"); 13 if(create_file == NULL){ 14 printf("ERROR"); 15 exit(1); 16 }else{ 17 fprintf(create_file, "%s", content); 18 printf("\nFile had been created please check the path: %s
", path); 19 fclose(create_file); 20 } 21 }

在使用strcat(const char*, const char*)注意要複製的空間大小