在线等待!如何把一个文件的内容读出并保存到另一个文件中

djwinter 2002-08-28 12:21:12
如何用最简单的方法把一个文件读出
然后在转存到令一个文件中?
只用说明文本文件即可
...全文
146 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
clack234 2002-08-29
  • 打赏
  • 举报
回复
#include <stdio.h>
main()
{ FILE *fp1,*fp2;
char c;
char *fname1="c:string1.txt",*fname2="d:\string1.txt";
if((fp1=fopen(fname1,"r"))==NULL)
{ printf("file can not open!\n");
exit(0);
}
if((fp2=fopen(fname2,"w"))==NULL)
{ printf("file can not open!\n");
exit(0);
}
c=fgetc(fp1);
while(!feof(fp1))
{ fputc(c,fp2);
c=fgetc(fp1);
}
rewind(fp1);
while(!feof(fp1))
putchar(fgetc(fp1));
fclose(fp2);
if((fp2=fopen(fname2,"r"))==NULL)
{ printf("file can not open!\n");
exit(0);
}
while(!feof(fp2))
putchar(fgetc(fp2));
fclose(fp1);
fclose(fp2);
}
以上这些就可以了。我试过了。没问题。希望你能用上。
kodo 2002-08-29
  • 打赏
  • 举报
回复
用管道可以哇?
我没有试过
djwinter 2002-08-29
  • 打赏
  • 举报
回复
希望用c语言解决
sjd163 2002-08-28
  • 打赏
  • 举报
回复
潭浩强《C程序设计》里面有例子
beckhamlt 2002-08-28
  • 打赏
  • 举报
回复
《C++ PRIMER》里面有很多例子
无聊客 2002-08-28
  • 打赏
  • 举报
回复
用CFile好了,呵呵,还是旧货好用
djwinter 2002-08-28
  • 打赏
  • 举报
回复
比如我从一个文件中的内容完全加到另一个文本中
好像就不行了呀
blh 2002-08-28
  • 打赏
  • 举报
回复
linux:
cat file1.txt > file2.txt

windows:
type file1.txt > file2.txt
yanyanyan 2002-08-28
  • 打赏
  • 举报
回复
system("cp srcfile.txt dstfile.txt");
beckhamlt 2002-08-28
  • 打赏
  • 举报
回复
这个程序从一个名为in_file的文本文件读取单词,然后把每个单词写到一个名为out_file的输出文件中,并且每个单词之间用空格分开
#include <iostream>
#include <fstream>
#include <string>
int main()
{
ofstream outfile("out_file");
ifstream infile("in_file");
if (! infile)
{
cerr <<"error:unable to open input file !\n";
return -1;
}
if(! outfile)
{
cerr <<"error:unable to open output file !\n";
return -2;
}
string word;
while (infile >> word )
outfile << word << ' ';
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧