【问题标题】:Reading and comparing lines from a text-file从文本文件中读取和比较行
【发布时间】:2011-11-22 05:51:17
【问题描述】:

我有这段代码,我想从文本文件中读取行并从该行中查找唯一代码。这是我的文本文件中的一些内容:

AGU UAC AUU GCG CGA UGG GCC UCG AGA CCC GGG UUU AAA GUA GGU GA

GUU ACA UUG CGC GAU GGG CCU CGA GAC CCG GGU UUA AAG UAG GUG A

UUA CAU UGC GCG M GGC CUC GAG ACC CGG GUU UAA AGU AGG UGA

UGG M AAA UUU GGG CCC AGA GCU CCG GGU AGC GCG UUA CAU UGA

我想找到包含字母'M' 的行并将它们分开,以便我可以将它们进一步分解并进行比较。不过我有点麻烦。 我试图找到它们并将其分配给一个字符串,但它似乎将所有行分配给同一个字符串。这是我目前所拥有的:

ifstream code_File ("example.txt");   // open text file.
if (code_File.is_open()) {
   while (code_File.good()) {
      getline(code_File,line);    //get the contents of file 
      cout  << line << endl;     // output contents of file on screen.
      found = line.find_first_of('M', 0);               // Finding start code
      if (found != string::npos) {
         code_Assign.assign(line, int(found), 100);        
         /* assign the line to code_Assign and print out string from where I 
            found the start code 'M'. */
         cout << endl << "code_Assign: " << code_Assign << endl << endl;

ED:我应该使用字符串替换而不是分配吗?

【问题讨论】:

  • 分配给code_Assign 时要达到什么目的?到目前为止我看到的问题是assign 将覆盖任何code_Assign 每次都有。如果您尝试收集所有要使用字符串向量的行。
  • 所以当我在我的文本文件中找到第一个“M”时,我想将整行(以及那一行)作为字符串分配给 Code_Assign 并打印出来。这也是我每次看到“M”之后都想做的事情,但它们都需要是不同的字符串。你如何使用字符串向量?我还没有学过向量...。
  • 我没有看到任何明显的错误。请注意,虽然您没有将整行分配给 code_Assign,但您只是在 M 的位置之后分配了 100 个字符。
  • 你需要一个字符串向量,建议你开始学习它们。
  • 您还应该修复您的 while 循环 while (code_file.good()) { getline(code_File,line) ... } 是错误的。仅仅因为您的文件很好并不意味着您可以从中读取一行。正确的循环是while (getline(code_file,line) { ... }。这个循环测试你是否成功地从文件中读取了一行,这就是你想要的。

标签: c++


【解决方案1】:

您每次迭代都重写code_Assigncode_Assign.assign(line, int(found), 100); 为字符串分配来自line 内容,之前的内容丢失。使用替换也不行。您需要在某处存储字符串,最简单的方法是使用vector

您声明一个空的字符串向量,如下所示:

std::vector<std::string> my_vector_of_strings;

与普通数组不同,vector 会在您向其添加元素时动态调整自身大小,因此您无需在编译时知道它需要多大。更多信息在这里:vector reference

接下来,

   while (code_File.good()) {
        getline(code_File,line); 

是错误的形式,之前在 SO 上已多次解释过(例如,here)。 在while 条件中移动getline() 调用。您的代码应如下所示:

// untested

ifstream code_File ("example.txt");   // open text file.
vector<string> vec_str;               // declare an empty vector of strings
string line;

if (code_File.is_open())
    while (getline(code_File, line)) { // read a line and only enter the loop if it succeeds 
        size_t found = line.find_first_of('M');  // you can omit the second parameter, it defaults to 0 
        if (found != string::npos) {
             line = line.substr(found); // take a substring from where we found 'M' to the end 
             vec_str.push_back(line);   // add the line to the vector
        }
    }

// print out the lines in vector:

for (size_t i = 0; i < vec_str.size(); i++)
    cout << vec_str[i] << endl;

// or, prettier, using the new c++11's range based for:

for (string s& : vec_str) cout << s << endl;

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多