c programming create a file

Posted zjhangia

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("
File had been created please check the path: %s", path);
19         fclose(create_file);
20     }
21 }

在使用strcat(const char*, const char*)注意要复制的空间大小

 



以上是关于c programming create a file的主要内容,如果未能解决你的问题,请参考以下文章