2014-11-20 133 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; 
}