简体   繁体   English

从输入文件到数组的混乱读取

[英]messy reading from input file into array

I've been workingo n this for a long time so I figured it's time to ask a question as I have no idea what's going on. 我已经为此工作了很长时间,所以我想是时候问一个问题了,因为我不知道发生了什么。 For whatever reason in this loop, it only works proper through the first time, then it goes back to the beginning of the file and starts to read from the beginning again. 无论在此循环中出于何种原因,它只能在第一次使用时才能正常工作,然后返回到文件的开头,并再次从头开始读取。 Thanks. 谢谢。

void readGasFile(int numTherms, int arrSize, string fileName, Customer cArray[])
 {
    cout << "reading gas file";

    int i;
    string temp;
    int temp2;

    ifstream inputFile;
    inputFile.open(fileName.c_str());

    if (inputFile.is_open())
    {
        cout << "reading input file";

        string dummyLine;
        getline(inputFile, dummyLine);

        //ignore first line

        /* *****************************************
        format:
        1212 <--- account number
        Lance, Ahmed
        1200  1212  <----- previous, current therms meter readings
        2323        <----- gas therms used
        ******************************************/

        for(i=0; i<arrSize; ++i)
        {
            cout << "start loop" << i << endl;

            inputFile >> temp2;
            cArray[i].setNumber(temp2);

            cout << temp2 << endl;;

            inputFile >> temp;
            cArray[i].setLastName(temp);

            inputFile >> temp;
            cArray[i].setFirstName(temp);

            inputFile >> temp2;
            cArray[i].setPrevious(temp2);

            inputFile >> temp2;
            cArray[i].setCurrent(temp2);

            inputFile >> temp2;
            cArray[i].setTherms(temp2);
        }
    }
    return;
}

Based on the code and without seeing the exact input it is impossible to tell but I'd bet that reading actually fails as some point. 基于代码并且没有看到确切的输入,这是无法分辨的,但是我敢打赌,阅读实际上在某些方面会失败。 Since you don't check your inputs as you always should, your program doesn't notice it. 由于您没有像往常一样检查输入内容,因此程序不会注意到它。 As a side node, you read the name including the comma which is probably also not desirable. 作为副节点,您读取的名称包括逗号,这可能也是不希望的。 Here is how I would write the loop: 这是我写循环的方式:

for(i = 0; i != arrSize
           && inFile >> number
           && std::getline(inFile, lastName, ',')
           && std::getline(inFile, firstName)
           && inFile >> previous
           && inFile >> current
           && inFile >> therms; ++i) {
    // set the various values
}

Obviously, the various variable would need to be declared appropriately. 显然,各种变量需要适当地声明。 Realistically, I would probably consider writing an input operator. 实际上,我可能会考虑编写输入运算符。 If you find that you can't read anything you'll need to find out where the input goes wrong, eg, by outputting that you successfully read the value after each subexpression, eg: 如果发现无法读取任何内容,则需要找出输入错误的地方,例如,通过输出在每个子表达式之后成功读取该值,例如:

for(i = 0; i != arrSize
           && inFile >> number && std::cout << "number=" << number << '\n'
           && std::getline(inFile, lastName, ',') && std::cout << "name='" << lastName << "'\n"
           && std::getline(inFile, firstName) && std::cout "first-name='" << firstName << '\n'
           && inFile >> previous && std::cout << "previous=" << previous << '\n'
           && inFile >> current && std::cout << "current=" << current << '\n'
           && inFile >> therms && std::cout << "therms=" << therms << '\n'; ++i) {
    // set the various values
}

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

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