关于回写文件,出错
thb28 2005-11-28 09:52:36 我先用ifsream一行行读出1.txt的内容,一行行存到str[]数组中,对str[]做相应的修改,想把文件覆盖重写,为什么为出错?
程序如下
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string a[]={"123","234","345"};
int index = 3;
ifstream infile("1.txt");
ofstream outfile("2.txt");
string temp[100];
int line = 1;
string str[10000];
int first,last;
while(getline(infile,str[line]))
{
line++;
}
for(int i = 0; i < line ; i++)
{
if(str[i].find("length($CONTROL_FILE)") != -1)
first = i;
if(str[i].find("$TX_DATE = substr $CONTROL_FILE,length($CONTROL_FILE)-12,8;")!=-1)
last = i;
}
int index1;
for( i = 0; i < index;i++)
temp[i] = a[i];
for( i = 0; i<line - last; i ++)
temp[i+index] = str[last+i+1];
for( i = first ; i <=last; i++)
str[i] = "";
for( i = 0; i<=index+line-last; i ++)
{str[i+first] = temp[i];
index1 = i;
}
infile.close();
for(i=0; i <index1+first; i++)
outfile<<str[i]<<endl;
return 1;
}