簡體   English   中英

從文本文件中讀取大塊行到字符串

[英]Reading chunks of line from text file into strings

所以說如果我在文本文件中有這一行。

AarI/CACCTGCNNNN'NNNN/'NNNNNNNNGCAGGTG//

我想要的是將這行讀入字符串,直到出現正斜杠,然后開始將下一組字符讀入另一個字符串。因此,在此示例中,我將包含3個字符串

string1 =  "AarI"
string2 = "CACCTGCNNNN'NNNN"
string3 = "'NNNNNNNNGCAGGTG"

任何想法如何去做?

帶有'/'字符的istream::getline() -請參閱: http : //www.cplusplus.com/reference/istream/istream/getline/

不是最好或最安全的方法,可能是較簡單的方法中的方法。

使用sstream 下面的代碼顯示了如何分割字符串的示例。

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

vector<string> split(string str, char delimiter);

int main(int argc, char **argv) {

  string DNAstr = "AarI/CACCTGCNNNN'NNNN/'NNNNNNNNGCAGGTG//";
  vector<string> splittedlines = split(DNAstr, '/');

  for(int i = 0; i < splittedlines.size(); ++i)
    cout <<""<<splittedlines[i] << " \n";

  return 0;
} 


vector<string> split(string str, char delimiter) {
  vector<string> buffer;
  stringstream ss(str); 
  string tok;

  while(getline(ss, tok, delimiter)) {
    buffer.push_back(tok);
  }

  return buffer;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM