首页 > 其他 > 详细

c programming create a file

时间:2019-01-06 23:33:53      阅读:155      评论:0      收藏:0      [点我收藏+]
 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*)注意要复制的空间大小

 

c programming create a file

原文:https://www.cnblogs.com/zjhangia/p/10230867.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!