欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

linux c之把最简单的字符串数据追加写入文件

发布时间:2023/12/4 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux c之把最简单的字符串数据追加写入文件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1 问题

把最简单的字符串数据追加写入文件

 

 

 

 

2 代码实现

#include <stdio.h> #include <string.h>void write_data_to_file(const char *path, char *str) {FILE *fd = fopen(path, "a+");if (fd == NULL) {printf("fd is NULL and open file fail\n");return;}printf("fd != NULL\n");if (str && str[0] != 0) {fwrite(str, strlen(str), 1, fd);char *next = "\n";fwrite(next, strlen(next), 1, fd);}fclose(fd); }int main() {char *path = "/home/chenyu/Desktop/linux/wf/c.txt";char *str = "chenyu";char *str1 = "hell word";char *str2 = "write data to file";write_data_to_file(path, str);write_data_to_file(path, str1);write_data_to_file(path, str2);return 0; }

 

 

 

3 运行结果

gcc -g write.c -o write ./writevim c.txtchenyu hell word write data to file

 

总结

以上是生活随笔为你收集整理的linux c之把最简单的字符串数据追加写入文件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。