我有從程序的TSV文件管分隔文件,但是我有在那裏它們放置不同的信息在由所述管限定符號一個小區的問題。迭代通過標籤,然後使用C++
XP_017347145.1 GO:0003676|GO:0005524|GO:0006139|GO:0008026|GO:0016818
XP_017347145.1 GO:0003677|GO:0004003|GO:0005524
XP_017347145.1 GO:0005524
XP_017347145.1 GO:0004003|GO:0016818
XP_017347145.1 GO:0003676|GO:0005524|GO:0006139|GO:0008026|GO:0016818
XP_017350967.1 GO:0005515
我想將它轉換成只有兩列像下面,但它似乎我不理解如何使用,則對getline()函數在C++中。
我有經驗其實並不多,但輸出應該看起來象下面這樣:
XP_017347145.1 = GO:0003676
XP_017347145.1 = GO:0005524
XP_017347145.1 = GO:0006139
XP_017347145.1 = GO:0008026
XP_017347145.1 = GO:0016818
XP_017347145.1 = GO:0003677
XP_017347145.1 = GO:0004003
XP_017347145.1 = GO:0005524
XP_017347145.1 = GO:0005524
XP_017347145.1 = GO:0004003
XP_017347145.1 = GO:0016818
XP_017347145.1 = GO:0003676
XP_017347145.1 = GO:0005524
XP_017347145.1 = GO:0006139
XP_017347145.1 = GO:0008026
XP_017347145.1 = GO:0016818
XP_017350967.1 = GO:0005515
我在C++當前代碼失敗,錯過在某些地方等號,並返回一個標籤來代替。
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
int main() {
using namespace std;
string stringIn;
string stringOut;
string value;
string value2;
cout << "Input the name of the file: " << endl;
getline(cin, stringIn);
cout << "The output file name is " << endl;
getline(cin, stringOut);
ifstream inputFile(stringIn);
ofstream outputFile(stringOut);
// Let the user know if the file exists
if (!inputFile) {
cout << "Cannot open input file" << endl;
}
if (!outputFile) {
cout << "Can not save output file" << endl;
}
// It should iterate through the values using column
// and column2 delimited by the pipe sign.
// For example, GO:0005524|GO:0008026 and this could be of unknown length.
while (getline(inputFile,value,'\t')) {
while (getline(inputFile,value2,'|')) {
outputFile << value + " = " + value2 << endl;
}
}
outputFile.close();
inputFile.close();
cin.get();
return 0;
}
我現在的代碼返回下面的輸出和數據,如下所示。任何建議,將不勝感激。
GO:0016818\nXP_017347145.1\tGO:0003677
^
|
|
newline captured
所以然後它打印整個記錄而不等號,因爲它是先前俘獲value2
的一部分:因爲getline(inputFile,value2,'|')
正在捕獲以下會發生
XP_017347145.1 = GO:0003676
XP_017347145.1 = GO:0005524
XP_017347145.1 = GO:0006139
XP_017347145.1 = GO:0008026
XP_017347145.1 = GO:0016818
XP_017347145.1 GO:0003677
XP_017347145.1 = GO:0004003
XP_017347145.1 = GO:0005524
XP_017347145.1 GO:0005524
XP_017347145.1 GO:0004003
XP_017347145.1 = GO:0016818
XP_017347145.1 GO:0003676
XP_017347145.1 = GO:0005524
XP_017347145.1 = GO:0006139
XP_017347145.1 = GO:0008026
XP_017347145.1 = GO:0016818
XP_017350967.1 GO:0005515
問題是什麼? –