2013-04-15 2 views
-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 
} 
+1

"ifstream, ofstream을 사용하고 싶지 않습니다." 당신은 여러 곳에서 그것을 사용하고 있습니다. 두 개 이상의 입력 파일을 사용하고 있습니까? – 999k

답변

0

내가 내 문제의 해결책을 가지고 ... 지금 내 파일은 ... 제대로

을 분석 점점하지만 난 생각합니다. 나는 여기에 내 programm을 게시해야합니다.

이렇게하면됩니다.

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. 
관련 문제