文件的拷贝

pingfann 2003-08-30 06:15:36
我想把input里的内容拷贝到outputl,下面的程序不能够实现,请指教
#include "iostream.h"
#include <fstream.h>
#include <string.h>
int main()
{
ofstream outfile("output.txt");
ifstream infile("input.txt");
if(!infile)
{
cerr<<"Error:Unable to open the input file"<<endl;
return -1;
}

if(!outfile)
{
cerr<<"Error:Unable to open the output file"<<endl;
return -2;
}
char word;
while(infile>>word)
cout<< outfile <<word;
return 0;
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
紫郢剑侠 2003-08-31
  • 打赏
  • 举报
回复
UP
huzhaoliang 2003-08-31
  • 打赏
  • 举报
回复
ifstream infile("input.txt",ios::binary);

if(!infile)
{
cerr<<"Error:Unable to open the input file"<<endl;
return -1;
}
当input不存在时,并没有输出错误信息,而是产生了一个新的input 文件,这是什么原因。
huzhaoliang 2003-08-31
  • 打赏
  • 举报
回复
subtop你好!
while(!_kbhit()){};
上面这句话的作用是什么?而且我使用时系统老是报错,为什么?
对了,我想在屏幕上显示我所拷贝 的内容应该怎样做?请指教
subtop 2003-08-31
  • 打赏
  • 举报
回复
int _kbhit() 是 conio.h 里声明的一个库函数,它返回键盘输入所对应的虚拟码。
while(!_kbhit()){}
使你的程序在运行完该语句前的最后一条语句后处于等待状态,直到有键盘输入,跳出while循环,继续程序流程。
subtop 2003-08-31
  • 打赏
  • 举报
回复
在使用
ifstream( const char* szName, int nMode = ios::in, int nProt = lebuf::openprot );

构造ifstream对象时,nMode有三个Mode标志位可选:
ios::in,文件被用作输入而打开,是缺省的!
ios::binary,以二进制模式打开文件(缺省为文本模式)
ios::nocreate,文件不存在时,不创建新文件,而是返回错误。
如果想要测试输入文件是否存在的话,必须设置ios::nocreate.
比如在此,你可以设置nMode为 ios:binary|ios::nocreate
subtop 2003-08-30
  • 打赏
  • 举报
回复
1)把 cout<< outfile <<word; 改为 outfile <<word;

2)由于<<是“提取”运算符,因此它去除了分割符(delimiters),如果要是两个文件完全一样,即保留所有分割符,就要用 int istream::peek(),我修改了你的程序,使得他复制后的两个文件完全一样。

3)文件要以二进制形式打开。

修改后程序如下:
ofstream outfile("output.txt",ios::binary);
ifstream infile("input.txt",ios::binary);
if(!infile)
{
cerr<<"Error:Unable to open the input file"<<endl;
return -1;
}

if(!outfile)
{
cerr<<"Error:Unable to open the output file"<<endl;
return -2;
}
int word;
word = infile.peek();
while(word!=EOF)
{
outfile<<(char)word;
infile.seekg(1L,ios::cur);
word = infile.peek();
}
outfile<<flush;
while(!_kbhit())
{}
return 0;


luolovegui 2003-08-30
  • 打赏
  • 举报
回复
cout<< outfile <<word;错了
要这样写。outfile <<word;
就可以了,但说实在的,这样的方法不好。

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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