读书人

为什么读取文件文件内容会被改掉解决

发布时间: 2012-02-21 16:26:23 作者: rapoo

为什么读取文件,文件内容会被改掉
/*大家看看下面这段程序,为什么我每一次运行程序以后,order.txt中的内容会被修改,一般都是第一行的第一个字符会被拷贝一次,例如原文件是:3 5 7 dall
运行程序以后,便成了:3 3 5 7 dall了。*/
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

typedef struct{
int opr_type;
int time_loop;
int time_bet;
string path;
}myCtr;

int main(void)
{
myCtr ctr1;
fstream file1, file2;

file2.open( "order.txt ");
file1.open( "test.txt ");

file2 > > ctr1.opr_type
> > ctr1.time_loop
> > ctr1.time_bet
> > ctr1.path;

file1 < < ctr1.opr_type < < ' '
< < ctr1.time_loop < < ' '
< < ctr1.time_bet < < ' '
< < ctr1.path < < endl;

file1.close();
file2.close();

return 0;
}



[解决办法]
ofstream file1
ifstream file2;

[解决办法]
你换个编译器再试。

读书人网 >C++

热点推荐