c++中如何在txt文件中每行末添加数据

比如1111122222333334444455555改为111110222220333330444440555550
2025-03-04 13:37:23
推荐回答(3个)
回答1:

#include 
#include 
main()
{
FILE *fp = NULL,*fw=NULL;
char str[100]={0};
fp = fopen("data.txt","r");
fw = fopen("result.txt","w");
while(fgets(str,sizeof(str),fp))
{
str[strlen(str)-1]='0';
strcat(str,"\r\n");
fputs(str,fw);
}
fclose(fp);
fclose(fw);
}

回答2:

按行读取原来的文件内容,修改后写到新的文件里即可。

回答3:

理解换行符就可以了。十六进制表示为0x0d0a。要在行尾添加数据,只要在换行符之前添加就好。