2014-11-20 128 views
0

我有一堆基於ASCII的文本文件,用作各種計算機程序的輸入文件,我需要將它們轉換爲不同的格式。每個輸入文件以4位數字開頭,然後如果前四位數字以0開頭(數字0),則後面跟隨其他輸入數據或註釋行。我正在開發一個基於C++的文件轉換器,我希望它讀取四位數的數字,如果這個數字在它後面的註釋行中讀取爲零。下面提供了一個例子。 C++可以很容易地讀取數組或通過使用std :: vector;然而讀取字符串會變得複雜得多。首先,如果每條評論行的單詞數量相同,我可以將每個字符串視爲在固定列中填充自己的行,但由於每條評論行的單詞數量不同,因此需要的列數在每一行讀入會有所不同。有沒有一種簡單的方法來閱讀註釋行,C++不會將每個單詞之間的空格視爲一列數據的末尾和另一列的開頭?通用數字和數據用於下面的文件示例中,但希望您可以看到以0開頭的註釋行跟隨它們的單詞數量不同,因此無法將該文件作爲嚴重的數據列讀取。用C++讀取各種長度的多個文本字符串

0001 Input File Name 
0001 - Description of input file goes here 
0001 - PROGRAM name that works on this data 
0000 ========================================== 
0001 List of references used in the development of this input file 
0001 [1] Ref. 1 
0001 [2] Ref. 2 
0001 [3] Ref. 3 
1100 Input line 1:  CBRD 1-0220 
1101 Core Length (mm): 8.189 
1102 Core diameter (mm): 37.81 
+2

請縮短_story_,加上實際_codes_你的努力和你的_relevant_問題 – P0W 2014-11-20 17:59:11

+0

你可以用'的std ::函數getline()'讀取整條生產線。 – SHR 2014-11-20 18:05:27

+0

將字符串讀入字符串,然後根據您的規則解析字符串。試圖讓一個文件閱讀器成爲一個解析器(就像你試圖做的那樣)過於複雜。 – PaulMcKenzie 2014-11-20 18:14:03

回答

2

使用getline函數從文件讀取一行到一個字符串,並對該字符串進行處理以完成您想要的任何操作。

喜歡的東西:而(函數getline(文件,字符串)){...}

你不需要知道每行最多字符數。 這簡直就是我的意思:根據上面提供的建議

int main() { 
     std::fstream iFile("Input.txt", std::fstream::in); 
     //You might want to check if it is open 
     std::string line; 

     int firstNumber; 
     std::string word; 
     while(getline(iFile, line)){ 
     std::stringstream lineStream(line); 

     lineStream >> firstNumber; 
     if(firstNumber == 0) { // modify based on what you want to do 
      while(lineStream >> word) { 
      std::cout << word << " "; 
      } 
     } 
     } 
     std::cout << std::endl; 
     iFile.close(); 
    } 
0

,我能夠實現以下解決方案。我會盡力清理它,使其更通用,以便可以輕鬆應用於其他問題。我很欣賞這些建議和它給我的幫助。

#include <iostream> 
#include <fstream> 
#include <cstring> 
#include <stdio.h> 

int main(int argc, const char * argv[]) { 

    std::string Input_File("Input.txt"); 
    std::string comment; 
    const int MAX_CHARS_PER_LINE = 1200; 
    const int MAX_TOKENS_PER_LINE = 40; 
    const char* const DELIMITER = " "; 
    FILE *Output_File; 

    std::ifstream inp(Input_File, std::ios::in | std::ios::binary); 
    if(!inp) { 
     std::cout << "Cannot Open " << Input_File << std::endl; 
     return 1; // Terminate program 
    } 

    Output_File = fopen ("Output_File.txt","w"); 
    std::ofstream out("Output_File.txt", std::ios::in | std::ios::binary); 

    // read each line of the file 
    while (!inp.eof()) 
    { 
     // read an entire line into memory 
     char buf[MAX_CHARS_PER_LINE]; 
     inp.getline(buf, MAX_CHARS_PER_LINE); 

     // parse the line into blank-delimited tokens 
     int n = 0; // a for-loop index 

     // array to store memory addresses of the tokens in buf 
     const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0 

     // parse the line 
     token[0] = strtok(buf, DELIMITER); // first token 
     if (token[0]) // zero if line is blank 
     { 
      for (n = 1; n < MAX_TOKENS_PER_LINE; n++) 
      { 
       token[n] = strtok(0, DELIMITER); // subsequent tokens 
       if (!token[n]) break; // no more tokens 
      } 
      if (strncmp (token[0],"0",1) == 0) 
      { 
       for(int i = 0; i < n; i++) out << token[i] << " "; 
      } 
      out << std::endl; 
     } 
    } 
    inp.close(); 

    return 0; 
}