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*)注意要复制的空间大小

猜你喜欢

转载自www.cnblogs.com/zjhangia/p/10230867.html