English 中文(简体)
阅读和比较文本档案中的线
原标题:Reading and comparing lines from a text-file
  • 时间:2011-11-22 05:51:17
  •  标签:
  • c++

我有这一守则,我想读一下文本,并从这一行文中找到独特的代码。 这里,我的文本是:

AGU UACAUU GCG CGA UGG 海湾合作委员会

古阿姆集团

UUA CAU UGCG M GGC CUC GAGAC CGG GUUA AGU AGG UGA

UGG M AAUUU GG CCC AGA GCU CCG GGU AGCG UUA CAU

I want to find the lines that contain the letter M and make them separate strings so that I can break them up even more and compare them. I am having a little trouble though. I am trying to find them and the assign it to a string, but it seems to assign all the lines to the same string. This is what I have so far:

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:我是否应该用扼杀代替分配?

最佳回答

页: 1 <代码>代码_Assign.assign (line, int(found), 100); 指定带new content from line and the previous content is loss. 要么用掉得起。 您需要储存地体,最容易的方式是使用<条码>查询器/代码>。

你们宣布了这样的扼杀手段:

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

与普通阵列不同的是,当你给它增加内容时,病媒就会生动地转基因,这样你就不必知道在汇编时间需要多少份量。 更具体地说:vector reference

Next,

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

is bad form and it s been explained many times before on SO (here, for example). Move the getline() call in while condition. Your code should look something like this:

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

Hope that helps.

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签