简体   繁体   English

无法从输入文件正确读取数据

[英]Data not being correctly read from input file

please see below c++ function. 请参见下面的c ++函数。 The point of it is to store variables in an input file ("is") to global arrays. 这样做的目的是将变量存储在全局文件的输入文件(“ is”)中。 I go through with the debugger with a watch on the "temp" variable (the first if statement seems to work fine), and after reading the first line of the input file, the variable temp no longer updates. 我通过调试器观察了“ temp”变量(第一个if语句似乎可以正常工作),并且在读取输入文件的第一行之后,变量temp不再更新。

The file is in a specific format so it should be reading an int, although I did eventually put a char at the end of the KEYFRAME if statement to see if it was reading the endline character (it wasn't). 该文件采用特定格式,因此应该读取一个int值,尽管我最终确实将一个char放在KEYFRAME if语句的末尾,以查看它是否正在读取结尾字符(不是)。

What are some possible reasons for this? 有哪些可能的原因呢? Thank you so much! 非常感谢!

 void readFile(istream& is){
        string next;
        int j = 0;
        int i = 0;
    while (is){
    for (int i = 0; i < F; i++){ 
            is >> next;
            if (next == "OBJECT")
            {

                int num;
                is >> num;
                string name;
                is >> name;
                objects[j].objNum = num;
                objects[j].filename = name;
                j++;
            }
            else if (next == "KEYFRAME"){
                int k;
                int temp;
                is >> k;
                int time;
                is >> time;
                objects[k].myData[time].setObjNumber(k);
                objects[k].myData[time].setTime(time);
                is >> temp;
                objects[k].myData[time].setPosition('x', temp) ;
                is >> temp;
                objects[k].myData[time].setPosition('y', temp);
                is >> temp;
                objects[k].myData[time].setPosition('z', temp);
                is >> temp;
                objects[k].myData[time].setRotation('x', temp);
                is >> temp;
                objects[k].myData[time].setRotation('y', temp);
                is >> temp;
                objects[k].myData[time].setRotation('z', temp);
                is >> temp;
                objects[k].myData[time].setScaling('x', temp);
                is >> temp;
                objects[k].myData[time].setScaling('y', temp);
                is >> temp;
                objects[k].myData[time].setScaling('z', temp);
                char get;
                is >> get;
            }
            else {
                cout << "Error reading input file";
                return;
            }

        }
    }
}

The most common reasons why std::istream 's operator>> does not update the variable can be demonstrated using the following simple example: 使用以下简单示例可以证明std::istreamoperator>>不更新变量的最常见原因:

#include <sstream>
#include <iostream>

int main() {

    std::string sample_input="1 2 3 A 4 5";

    std::istringstream i(sample_input);

    int a=0, b=0, c=0, d=0;
    std::string e;
    int f=0;

    i >> a >> b >> c >> d >> e >> f;

    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;
    std::cout << d << std::endl;
    std::cout << e << std::endl;
    std::cout << f << std::endl;
}

The resulting output from this program is: 该程序的结果输出为:

    1
    2
    3
    0

    0

This example forces a conversion error on the fourth parameter. 本示例对第四个参数强制执行转换错误。 Once operator>> encounters a formatting conversion error, an error bit is set on the stream, which prevents further conversions. 一旦operator>>遇到格式转换错误,就在流上设置一个错误位,以防止进一步的转换。

When using operator>> , it's necessary to check for conversion errors after every input format conversion, and reset the stream's state, if necessary. 使用operator>> ,有必要在每次输入格式转换后检查转换错误,并在必要时重置流的状态。

So, your answer is that you have an input conversion error someplace. 因此,您的答案是您在某处存在输入转换错误。 I generally avoid using operator>> . 我通常避免使用operator>> It's fine for simple situations where the input is known, and there is no possibility of bad input. 这对于已知输入的简单情况很好,并且不会出现输入错误的可能性。 But as soon as you get into a situation where you might need to potentially handle bad input, using operator>> becomes painful, and its much better to take some other approach for parsing stream-based input. 但是,一旦遇到可能需要处理不良输入的情况,使用operator>>就很麻烦了,采用其他方法解析基于流的输入会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM