* 我已经回答了我的问题,并且运行正常。有任何人有同样的问题,请参阅第二个答案部分。 *
我必须逐行读取一个文件 ,并将输出逐行存储在另一个文件中, 但是我无法阅读所有的行,只读一行有三个记录。
或
请告诉我如何/何时使用ifstream/ofstream
。 file*
等
代码中是否有任何错误。
但它不起作用。
// initialization
int convertFileToSingleLine( char *p_record, char *sEor,long l);
char sEor[25]={"& 0000200222! MB00200"}; //end of record
ofstream outdata; //is it legal to initialize this here????
int main()
{
outdata.open("output.txt", ios::app); //output file
ifstream infile;
infile.open("sample.txt"); //inputfile
FILE *p_record=NULL;
char buf[4000];
long int lineC;
string line;
while (getline(infile, line))
{
lineC++;
}
lineC = 3 * lineC - 2; // every line has three records, first and last are header and footer
cout << lineC; //in my case file is having 20019 records
p_record = fopen("SWITCH.txt","r"); //input file for file pointer
for(int l = 1; l <= lineC; l++)
{
while ((fgets(buf, sizeof buf, p_record)) != NULL)
{
convertFileToSingleLine(buf, sEor, lineC);
}
}
fclose(p_record);
return 0;
}
int convertFileToSingleLine(char *sInFile, char *sEor, long lC) //convert into single line
{
//ofstream outdata;
//outdata.open("output.txt", ios::app);
char sTemp[1087] = "\0";
FILE *fpIn=NULL, *fpOut=NULL;
static long int l = 0;
char *ch = NULL;
char *ch1 = NULL;
//int l=0;
try
{
static int pos=0;
int c;
//outdata<<"HI TO";
while(l!=lC)
{
if( (ch=strstr(sInFile+pos, "001")) != NULL)
{
//if((ch1=strstr(ch, "& 00002! AB00200"))!=NULL)
//{
strncpy(sTemp, ch, 1086); //one record is 1086 charater long
//}
pos = pos + 1086; //move to next position in one line, by 1086 chacters.
outdata << sTemp;
}
l++;
}
return 0;
}
catch(char *str1)
{
cout << "ERROR while opening" << sInFile << endl;
}
return 0;
}
它只读取一行,并且读取第一条记录两次,然后是第二条记录。 我需要整个文件。 我必须删除那些ifstream和ofstream,这是他们的另一种方式。
**sample file:**
{
three records in one line:
002 ahjfdghfuisyguigeuihgjkgfjkbjbgjbfggfdbjbhj & 00002! AB00200 001 uyrugjnbfhdgyudgfshdfj & 00002! AB00200 001 ytygfhvghghjbv uhruighvluhjkl heuifhuihfuh & 00002! AB00200
001 jhsdjkagfdsf .....
}
output:=>
{
001 uyrugjnbfhdgyudgfshdfj & 00002! AB00200
001 uyrugjnbfhdgyudgfshdfj & 00002! AB00200
001 ytygfhvghghjbv uhruighvluhjkl heuifhuihfuh & 00002! AB00200
}
答案 0 :(得分:0)
我解决了我的问题...... 现在我的文件正在被正确解析...
但我想。我应该在这里发布我的程序。
所以,你可以做到这一点。
using namespace std;
void convertFileToSingleLine( char *p_record, char *sEor,long l);
char sEor[25]={"& 0000200222! MB00200"};
ofstream outdata;
static int pos=0;
int main()
{
outdata.open("output.txt", ios::app);
ifstream infile;
infile.open("sample.txt");
FILE *p_record=NULL;
char buf[4000]; // char buffer to store one whole record
long int lineC;
string line;
while (getline(infile, line))
{
lineC++; // count the number of lines
}
cout<<lineC<<endl; //23066 lineC
p_record= fopen("sample.txt","r");
while ((fgets(buf,sizeof buf, p_record)) != NULL) // fetching one line in buf from file pointer p_record
{
convertFileToSingleLine(buf, sEor, lineC);
}
fclose(p_record);
return 0;
}
void convertFileToSingleLine( char *sInFile, char *sEor, long lC)
{
ofstream outdata;
outdata.open("output.txt", ios::app);
char sTemp[ 1087]="\0";
FILE *fpIn=NULL, *fpOut=NULL;
static int l=0;
char *ch = NULL;
char *ch1 = NULL;
pos=0;
int val;
try
{
while(true)
{
if( (ch=strstr(sInFile+pos, "001086")) != NULL)
{
strncpy(sTemp, ch, 1086);
}
pos=pos+1086;
outdata<<sTemp<<endl;
if(pos>=3257)
{
break;
}
}
}
catch(char *str1)
{
cout<<"ERROR while opening"<<sInFile<<endl;
}
}
我的文件大小为1GB。现在它被正确解析了。
sample file:
{
three records in one line:
002 ahjfdghfuisyguigeuihgjkgfjkbjbgjbfggfdbjbhj & 00002! AB00200 001 uyrugjnbfhdgyudgfshdfj & 00002! AB00200 001 ytygfhvghghjbv uhruighvluhjkl heuifhuihfuh & 00002! AB00200 \\next line
001 jhdgfjksdkhv .... \\next line
001 jhsdjkagfdsf ..... \\next line
} //23066 lines in my one file, each having 3 records.
output:=>
{
001 uyrugjnbfhdgyudgfshdfj & 00002! AB00200 \next line
001 uyrugjnbfhdgyudgfshdfj & 00002! AB00200 \next line
001 ytygfhvghghjbv uhruighvluhjkl heuifhuihfuh \next line
001 jhdgfjksdkhv .... \\next line
001 jhsdjkagfdsf ..... \\next line
} //69198 record i got in my outputfile.