简体   繁体   English

在C ++中一次从一行文本文件读取int的麻烦(先输入char,然后进行转换)

[英]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. 我在这里以及cplusplus.com和各种资源上阅读过不同的帖子/问题,但在阅读文本文件方面我的代码仍然遇到问题。 The text file has one int (single digit) on each line. 文本文件的每一行都有一个int(一位数字)。 So for example: 5 1 2 5 . 例如: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? 我遇到麻烦了,我相信是'\\ n'这个字符吗? Because it will read the first int but then the second one and a few others will be -38!! 因为它将读取第一个int,但随后的第二个和其他几个将是-38! I've tried the ignore() thing (although maybe I did that wrong?) and that didn't work. 我已经尝试过ignore()事情(尽管也许我做错了吗?),但是那没有用。 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? 您正在从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(); 该参考中 ,函数istream::get()具有以下原型: int get(); I think you can see how that might not work in your program. 我认为您可以看到它在您的程序中可能不起作用。

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

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