在c ++中读取文件中的奇怪输出

时间:2013-11-12 16:50:04

标签: c++ string fstream stringstream

  

CHROMOSOME_IQ AllQele

     

CHXROMOSOME_IA AlleSle

     

CHRCOMOSOME_IZ AllelCe

我正在从文件中阅读上述内容。

void test(ifstream*, string*, string*, string*, string*, string*);
void read_file(char*);
int main(int v, char** argv)
{
    if (v != 2)
    {
        cout << "Files missing";
        exit(EXIT_FAILURE);
    }
    // calling method 
    read_file(argv[1]);
}
void read_file(char* j)
{

    string line, TChr, TType1, TType2, tUnk, TInfo;
    vector<pair<string, string> > TName;

    char t_strand, t_offset;
    int t_loc1, t_loc2;

    ifstream fileH;
    fileH.open(j);
    int count = 0;
    test(&fileH, &TChr, &TType1, &TType2, &tUnk, &TInfo);

    while (!fileH.eof())
    {
        ++count;
        test(&fileH, &TChr, &TType1, &TType2, &tUnk, &TInfo);

    }
    fileH.close();

    cout << count << "\n";
}
void test(ifstream* h, string* chr, string* tt1, string* tt2, string* tUnk, string* Tinfo)
{

    string line;
    getline(*h, line);

    stringstream line_tab(line);

    getline(line_tab, *chr, '\t');
    cout << "The value is " << *chr << "\n";

    getline(line_tab, *tt1, '\t');
    cout << "The value is " << *tt1 << "\n";
}

这些行是三行,但是输出为4。 我无法弄清楚,为什么会这样。 eof应该完美地工作,而不是在第4次迭代中给出mein null值。

输出:

  

值为CHROMOSOME_IQ
值为AllQele

值   是CHXROMOSOME_IA
值是AlleSle
值是   CHRCOMOSOME_IZ
值为AllelCe
值为该值   是AllelCe

另外,在传递文件句柄和行时,我必须使用stringstream,否则,在调用函数void test时我将无法读取。

0 个答案:

没有答案