-2
* 내 질문에 답변 해 주었고 정상적으로 실행되고 있습니다. 같은 문제가있는 사람은 두 번째 대답 부분을 참조하십시오. *파일 포인터를 통해 파일을 한 줄씩 읽는 중 문제가 발생했습니다.
나는, 다른 파일에 하나 개의 파일을 을 라인으로 라인에 의해 라인 및 저장 출력 라인을 읽을 필요하지만 난 단지 세 가지를 가진 한 줄을 읽는 모든 라인을 읽을 수 아니다 기록.
또는
방법을 말해/때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
}
"ifstream, ofstream을 사용하고 싶지 않습니다." 당신은 여러 곳에서 그것을 사용하고 있습니다. 두 개 이상의 입력 파일을 사용하고 있습니까? – 999k