使用getline和istringstream从文件中读取正在生成零

时间:2012-02-17 01:20:55

标签: c++ string file-io

我有一个看起来像这样的文本文件:

6
1 2 3 4 5 6
1 2
1 4
1 5
2 3
2 5
2 6
3 4
3 6
4 5
5 6

我使用以下内容将文件行读入我的变量和数组:

//Read first line
if( std::getline(in, line))                                                                                                                                                                                                              
    {                                                                                                                                                                                                                                        
        std::istringstream iss(line);                                                                                                                                                                                                        
        while( iss >> a);                                                                                                                                                                                                           
    }                                                                                                                                                                                                                                        

    // read second line                                                                                                                                                                                                 
    int i = 0;                                                                                                                                                                                                                               
    if( std::getline(in, line) )                                                                                                                                                                                                             
    {                                                                                                                                                                                                                                        
        std::istringstream iss(line);                                                                                                                                                                                                        
        while( iss >> b[i] )                                                                                                                                                                                                          
        {                                                                                                                                                                                                                                    
            ++i;                                                                                                                                                                                                                             
        }                                                                                                                                                                                                                                    
    }                                                                                                                                                                                                                                        

    // read rest of file matrix                                                                                                                                                                               
    int x = 0;                                                                                                                                                                                                                               
    while( !in.eof())                                                                                                                                                                                                                        
    {                                                                                                                                                                                                                                        
        if( std::getline(in, line))                                                                                                                                                                                                          
        {                                                                                                                                                                                                                                    
            std::istringstream iss1(line);                                                                                                                                                                                                   
            while( iss1 >> c[x])                                                                                                                                                                                                         
            {                                                                                                                                                                                                                                
                ++x;                                                                                                                                                                                                                         
            }                                                                                                                                                                                                                                
        }                                                                                                                                                                                                                                    
    }     

在上面的代码中a是一个int,bc是int数组,in是一个istream运算符,linestd::string 1}}。它似乎工作正常但是当我std::cout变量和数组时,我得到以下结果:

6
1,2,3,4,5,6
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

由于某种原因,数组c似乎用全零填充而不是文件中的数据。这对我来说很奇怪,因为我正在以完全相同的方式阅读前两行,他们似乎读得很好。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我终于想通了,我想我会把它发布在这里给未来的人们。我最终实现了下面的功能,它运行正常。

// declare the variables to be used
    int a, b, c, d, e;
    std::string line;

    // open file for reading
    in.open(file);

    in >> a;
    std::cout << a << '\n';

    for(int i=0; i<a; ++i)
        in >> b

    in >> c;

    for(int i = 0; i < c; ++i)
    {    
        in >> d >> e;

        // push to vector here
    }

    in.close();
相关问题