C++ 读取和比较文本文件中的行

C++ 读取和比较文本文件中的行,c++,C++,我有一个代码,我想从文本文件中读取行,并从该行中找到唯一的代码。下面是我的文本文件的一些内容: 美国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国 GU ACA UUG CGC GAU GGG CCU CGA GAC CCG GGU UUA AAG UAG GU A UUA CAU U

我有一个代码,我想从文本文件中读取行,并从该行中找到唯一的代码。下面是我的文本文件的一些内容:

美国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国联合酋长国

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

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

UGG M AAA UUU GGG CCC AGA GCU CCG GGU AGC GCG UU A 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;
ifstream代码_文件(“example.txt”);//打开文本文件。
if(代码\u文件.is\u open()){
while(code_File.good()){
getline(code_File,line);//获取文件的内容

不能每次迭代都重写
code\u Assign
code\u Assign.Assign(line,int(find),100);
使用
line
中的新内容分配字符串,以前的内容将丢失。使用replace也不行。您需要将字符串存储在某个位置,最简单的方法是使用
向量

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

std::vector<std::string> my_vector_of_strings;
// 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;
这是一种糟糕的形式,之前已经解释过很多次了(例如)。 移动
getline()
调用
while
条件。您的代码应该如下所示:

std::vector<std::string> my_vector_of_strings;
// 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;
//未经测试
ifstream code_文件(“example.txt”);//打开文本文件。
vector vec_str;//声明字符串的空向量
弦线;
if(代码\u文件.is\u open())
while(getline(code_File,line)){//读取一行并仅在成功时进入循环
size_t found=line.find_first_of('M');//您可以省略第二个参数,它默认为0
如果(找到!=字符串::npos){
line=line.substr(found);//从找到“M”的位置取一个子字符串,直到结束
vec_str.push_back(行);//将行添加到向量
}
}
//打印出矢量中的行:
对于(size_t i=0;i当分配给
code\u-Assign
时,您试图实现什么?到目前为止,我看到的问题是
Assign
将覆盖
code\u-Assign
每次拥有的任何内容。如果您试图收集要使用字符串向量的所有行。因此,当我在文本文件中找到第一个“M”时,我想分配w孔线(仅此线)作为一个字符串,我要分配并打印出来。这也是我每次看到“M”之后都要做的,但它们都需要是不同的字符串。如何使用字符串向量?我还没有学会向量…我没有看到任何明显的错误。请注意,虽然你没有将整行代码分配给“代码分配”,但你只是在分配g M位置后的100个字符。如果需要字符串向量,建议您开始学习它们。您还应该修复while循环
while(code\u file.good()){getline(code\u file,line)…}
错误。文件良好并不意味着您可以从中读取一行。正确的循环是
while(getline)(code_file,line){…}
。此循环测试您是否成功地从文件中读取了所需的行。