文件读取和添加到另一个文件,txt格式解决方案
文件读取和添加到另一个文件,txt格式
#include <IOSTREAM>
using namespace std;
int main()
{
FILE *fp1,*fp2;
char s[100];
int sum=100;
if ((fp1=fopen("string.txt","rt"))==NULL)
{
cout<<"con not open file!"<<endl;
exit(0);
}
fgets(s,sum,fp1);
if ((fp2=fopen("test.txt","at"))==NULL)
{
cout<<"can not open file!"<<endl;
exit(0);
}
fputs("\n",fp2);
fputs(s,fp2);
fgets(s,sum,fp2);
cout<<s<<endl;
fclose(fp1);
fclose(fp2);
return 0;
}
这程序有什么问题?请教一下,刚学了文件的操作!
结果是很多个屯字,其中我的这两个txt文件的内容是 “哈哈,行啦!” ”zhan de en"
------解决方案--------------------
if ((fp2=fopen("test.txt","at"))==NULL)
{
cout<<"can not open file!"<<endl;
exit(0);
}
at只能读不能写
------解决方案--------------------
char s[100];
后面没有用到的存储单元就会显示"屯"
可以尝试把数组长度动态分配
#include <IOSTREAM>
using namespace std;
int main()
{
FILE *fp1,*fp2;
char s[100];
int sum=100;
if ((fp1=fopen("string.txt","rt"))==NULL)
{
cout<<"con not open file!"<<endl;
exit(0);
}
fgets(s,sum,fp1);
if ((fp2=fopen("test.txt","at"))==NULL)
{
cout<<"can not open file!"<<endl;
exit(0);
}
fputs("\n",fp2);
fputs(s,fp2);
fgets(s,sum,fp2);
cout<<s<<endl;
fclose(fp1);
fclose(fp2);
return 0;
}
这程序有什么问题?请教一下,刚学了文件的操作!
结果是很多个屯字,其中我的这两个txt文件的内容是 “哈哈,行啦!” ”zhan de en"
------解决方案--------------------
if ((fp2=fopen("test.txt","at"))==NULL)
{
cout<<"can not open file!"<<endl;
exit(0);
}
at只能读不能写
------解决方案--------------------
char s[100];
后面没有用到的存储单元就会显示"屯"
可以尝试把数组长度动态分配