简体   繁体   中英

Trouble reading ints (well chars and then converting) from text file one line at a time in C++

I have read different posts/questions on here as well as cplusplus.com and various sources and am still having trouble with my code as far as reading a text file. The text file has one int (single digit) on each line. So for example: 5 1 2 5 . . . etc Each number is supposed to represent a specific variable. I am having trouble I believe it's with the '\\n' character? Because it will read the first int but then the second one and a few others will be -38!! I've tried the ignore() thing (although maybe I did that wrong?) and that didn't work. I'm sure it's a quick fix and probably staring me in the face but I can't see it! Here is the part of my code that I'm having issues with:

void RegOffice::analyzeFile()
{
    cout << "Please enter the location of the file to be read. " << endl;
    cin >> fileName;
    cout << endl;
    inData.open(fileName.c_str());

    if (inData.is_open())
    //while file is open
    {
        while(inData)       //While file is still good
        {
            char c;

            /* 
            The function below reads in one int at a time on each line. First line is the number of windows
            open. The next line will be the time (or clock tick) at which the next student(s) arrive. 
            The next line will be the number of students that arrive at that time. The lines after that
            will be the amount of time each student needs at a window in minutes.
            */

            c = inData.get();               //Gets the number of windows open
            winOpen = c - '0';  
            cout << "Windows Open: " << winOpen << endl;            
            windows = new GenQueue<Window>(winOpen);    //Sets up an array of open windows
            c = inData.get();
            time = c - '0';     //Gets the first time that students arrive
            cout << "Time: " << time << endl;   
            c = inData.get();
            numStudents = int(c - '0');     //Gets the number of students that enter the line at that time
            cout << "Number of Students: " << numStudents + 1 << endl;  

            for(int i = 0; i < numStudents; i++)    // numStudents is the number read in by the text file
            {
                Student *stu = new Student(); //creating a new instance of a student
                c = inData.get();
                stu->setTimeAtWin(c - '0');  //Setting student's wait time to the next number in file
                stu->setArrivalTime(time);   //Setting student's arrival time
                cout << "New Student created! Info for Student #" << i << ":" << endl;
                stu->print();
                line->addBack(*stu);   
            //Inserting that student to the back of the queue (The first student                                                       
            //will be first in line though if no one is in line)

            }
        }
     }
  }

Are you reading from an ifstream?

#include <fstream>
std::ifstream infile("inData.txt");

Then to read a line, do

int c;
infile >> c;

I see the problem now. From this reference the function istream::get() has this prototype: int get(); I think you can see how that might not work in your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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