职位关键字 工作地点
工作名称 公司名称 校园招聘
中国最好的工作搜索引擎!
  回复留言  
  一个C++程序问题
作者: zxdsg  01-01 08:00   回复  
 目的是把一个文件内容写给另一个,编译无错误,但就是写不到另一个文件里,请各位帮忙!!!原程序如下:

#include <iostream>

using namespace std;

#include <fstream>



using namespace std;

void main(){

fstream file1;

file1.open("Ex_DataFile.txt",ios::in);

if(!file1)

{

cout<<"error.\n";

return;

}

fstream file2;

file2.open("Ex_DatafileBak.txt",ios::out|ios::trunc);

if(!file2)

{

cout<<"error2.\n";

file1.close();

return;

}

char ch;

while (!file1.eof())

{

file1.read(&ch,1);

cout<<ch<<endl;

file2.write(&ch,1);

}

while(!file2.eof())

{

file2.read(&ch,1);

cout<<ch<<endl;

}

file2.close();

file1.close();

cout<<"\n";

}

  回复:一个C++程序问题
作者: yua2005  01-01 08:00   回复  
 写完文件后,关闭文件再重新以in打开

#include <iostream>

using namespace std;

#include <fstream>



using namespace std;

void main(){

fstream file1;

file1.open("Ex_DataFile.txt",ios::in);

if(!file1)

{

cout<<"error.\n";

return;

}

fstream file2;

file2.open("Ex_DatafileBak.txt",ios::out|ios::trunc);

if(!file2)

{

cout<<"error2.\n";

file1.close();

return;

}

char ch;

while (!file1.eof())

{

file1.read(&ch,1);

cout<<ch<<endl;

file2.write(&ch,1);

};

file2.close();

file2.open("Ex_DatafileBak.txt",ios::in);

while(!file2.eof())

{

file2.read(&ch,1);

cout<<ch<<endl;

}

file1.close();

cout<<"\n";

}

  回复:一个C++程序问题
作者: tomotao  05-21 16:18   回复  
  也有同样问题,

  回复:一个C++程序问题
作者: chenhaooo  05-21 16:18   回复  
  不要用file1.eof()来判断文件结束,这样会重复读最后一个字节;



#include <iostream>

using namespace std;

#include <fstream>



using namespace std;

void main(){

fstream file1;

file1.open("Ex_DataFile.txt",ios::in);

if(!file1)

{

cout<<"error.\n";

return;

}

fstream file2;

file2.open("Ex_DatafileBak.txt",ios::out|ios::trunc);

if(!file2)

{

cout<<"error2.\n";

file1.close();

return;

}

char ch;

while (file1.read(&ch,1))

{

cout<<ch<<endl;

file2.write(&ch,1);

cout << file2.eof();

};

file2.close();

file2.open("Ex_DatafileBak.txt",ios::in);

while(file2.read(&ch,1))

{

cout<<ch<<endl;

}

file1.close();

cout<<"\n";

}

标 题:
内 容:
用户名:
 游客
 
 
Copyright©2005-2007, 版权所有 WWW.JOBMET.COM
ICP证:京ICP备06019556号