码迷,mamicode.com
首页 > 其他好文 > 详细

c programming create a file

时间:2019-01-07 00:17:42      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:please   documents   char*   span   lease   lse   else   null   color   

 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

标签:please   documents   char*   span   lease   lse   else   null   color   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!