LINUX C语言写一个读写文件的程序,读取的话,要把文件内容显示出来,写的话,就是写进文件里面咯

LINUX C语言写一个读写文件的程序,读取的话,要把文件内容显示出来,写的话,就是写进文件里面咯,第1张

#include <stdio.h>

#include <stdlib.h>

int main()

{

FILE *pf = fopen("a.txt", "r")

if(pf == NULL) {

printf("open a.txt file failed!\n")

exit(0)

}

FILE *pf2 = fopen("b.txt", "w")

if(pf2 == NULL) {

printf("open b.txt file failed!\n")

fclose(pf)

exit(0)

}

char ch

while(!feof(pf)) {

ch = fgetc(pf)

putchar(ch)

fputc(ch, pf2)

}

fclose(pf2)

fclose(pf)

return 0

}

1. asctime(local)函数,是将struct tm*类型的参数转换成字符串类型,这样,方便fprintf函数将时间按照字符串输出来

2. “w+"是从文件开始写入。所以,每次重写都会覆盖以前的时间。可以使用"a+",在文件后面追加。即,将fp=fopen("time.txt", "w+")改成fp=fopen("time.txt", "a+")

3.有一个网站,可以查找linux的系统api函数

http://linux.die.net/


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/9024869.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-24
下一篇 2023-04-24

发表评论

登录后才能评论

评论列表(0条)

保存