c++ peek()看不到新行字符

C++ peek() not seeing new line character

本文关键字:新行 字符 看不到 peek c++      更新时间:2023-10-16

我在阅读一个文件的输入时遇到了一些麻烦,我真的不应该在家庭作业中遇到这个文件。我使用了非常类似的代码(唯一改变的是值被推入的地方)在先前的赋值。我得到了一个文本文件,输入如下:

10
3 6 4 9 0 7 8
8 5 3 7 3 4 -2
5 10 2 8 1 4 1
2 6 -3 1 3 7 1
1 10 -1 2 2 4 -2
10 9 -3 1 3 7 2 5 1
7 3 0 10 1 2 1 8 2
9 6 6 3 4 10 7
4 8 5 1 9 5 6
6 2 4 3 0 9 0

第一行是上面图中存在的顶点数。在后面的每一行,第一个数字是它所对应的顶点,下一个数字是它所连接的顶点,后面的数字是那条边的权值。这条线重复顶点,权值直到线的末尾(即,第一条线是顶点3,它有一条权值为4的边到6,一条权值为0的边到9,等等)。我用一个一维向量来表示一个矩阵用行主符号表示。我遇到的问题是,我的行变量似乎根本没有更新。目前,我从while循环的最后一行得到以下输出,它实际将数据插入到vector中。

3:  6: 4
3:  9: 0
3:  7: 8
3:  8: 5
3:  3: 7
3:  3: 4
3:  -2: 5
3:  10: 2
3:  8: 1
3:  4: 1
3:  2: 6
3:  -3: 1
3:  3: 7
3:  1: 1
3:  10: -1
3:  2: 2
3:  4: -2
3:  10: 9
3:  -3: 1
3:  3: 7
3:  2: 5
3:  1: 7
3:  3: 0
3:  10: 1
3:  2: 1
3:  8: 2
3:  9: 6
3:  6: 3
3:  4: 10
3:  7: 4
3:  8: 5
3:  1: 9
3:  5: 6
3:  6: 2
3:  4: 3
3:  0: 9
3:  0: 9

我的row变量似乎被卡住为3,就像input.peek()一样,while循环的条件永远不会看到新的行字符。真正令人困惑的部分是,在类似的任务中,这段代码可以很好地遍历输入文件和它们应该去的地方。我被难住了,所以如果有人能给我指出正确的方向,我会非常感激。如果我说得太啰嗦了,我先道歉。

我的代码如下。

if(input.is_open()) // making sure the input is open
{
    input >> nodeCount; //Grabbing the number of nodes from the first value of the file
    for(int i = 1; i < nodeCount*nodeCount; i++)
    {
        edgeList.push_back(graphNode());
        edgeList[i].value = infinity;
        edgeList[i].isInfinity = true;
        edgeList[i].pred = -1;
    }
    //Putting data from the file into the vector array
    while(!input.eof())
    {
        input >> row; //For each cycle through the list, we grab the first number on the line to get which x value (start vertex) we're working with
        while(input.peek() != 'n' && !input.eof())
        {
            input >> col;
            input >> edgeList[((row-1)*nodeCount)+(col-1)].value;
            edgeList[((row-1)*nodeCount)+(col-1)].isInfinity = false;
            edgeList[((row-1)*nodeCount)+(col-1)].pred = row;
            cout << row << ": " << " " << col << ": " << edgeList[((row-1)*nodeCount)+(col-1)].value << endl;
        }
    }
    input.close(); //Closing our input file since we don't need it anymore
}

从您输出的数字来看,很明显这个条件在文件结束之前不会计算为false:

input.peek() != 'n' && !input.eof()

我的问题是-你是使用windows风格,unix风格还是mac风格的行尾?也许有更好的方法来找出行结束的地方,不依赖于假设他们采取一定的ASCII值(s)?

阅读一行的最好方法是实际阅读一行。

std::string  line;
std::getline(input, line);

然后可以解析该行外的数字。

std::stringstream linestream(line);
int node;
linestream >> node;
int dst, weight;
while(linestream >> dst >> weight)
{
    // Do Stuff
}

你应该小心:

while(!input.eof())

是一个反模式。您必须小心,因为如果流进入错误状态,测试将永远不会变为真,从而可能使您陷入无限循环。

所以你的代码应该是这样的:
int linesToRead;
input >> linesToRead;    
std::string  line;
for(;std::getline(input, line) && linesToRead;--linesToRead)
{
    // If there is a chance of an empty line
    // you may want to check here.
    std::stringstream linestream(line);
    int node;
    linestream >> node;
    int dst, weight;
    while(linestream >> dst >> weight)
    {
        // Do Stuff
    }
}

手动关闭文件不是一个好的做法。

input.close();

可能将流设置为抛出错误。如果调用close()而它抛出会发生什么?即使你在正常模式下,它只是在close()上产生一个错误。你打算做什么?最好让析构函数关闭文件并为您处理错误条件。看到