我有从程序的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
问题是什么? –