2013-04-15 97 views
-2

* 我已經回答了我的onw問題,它運行正常。任何人都有同樣的問題,請看第二個答案部分。 *無法通過文件指針逐行讀取文件


我要讀一個文件一行線,並通過網上商店的輸出線在另一個文件中, 但我不能夠讀取所有它只是讀書有三個一行行記錄。

請告訴我如何/以及何時使用ifstream/ofstreamfile*等 代碼中有任何錯誤。

但它不工作。

// 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

我得到了我的問題的解決方案... 現在我的文件得到正確解析...

,但我認爲。我應該在這裏發佈我的程序。

所以,你可以做到這一點。

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.