开发者

Cannot read input file ifstream

I'm trying to read an input file formatted with two numbers on a l开发者_运维百科ine and store the first number in the line in one vector and the second number in another vector.

Every part of my code works fine except for the actual reading of the file.

I have put couts all over the place and it looks like while my file is being opened, it's not being read, so my vectors keep getting filled until I run out of memory.

Here's the part of my code that reads the file:

cout << "Input Filename: ";
cin >> input;
//open input file
inputdata.open(input.c_str());
if(!inputdata){
cerr << "Error: Unable to open file!" << endl;
}

while(!inputdata.eof()){
    counter++;
    hold = 0;
    if(counter > 0){
        inputdata >> hold;
        //cout << hold << endl;
        if(counter%2 != 0)
            data.push_back(hold);
        else
            weight.push_back(hold);
    }
}

(where counter is an integer initialized at -1 since there is a one-word title at the beginning of the input file that I need to ignore).

I know using .eof() is frowned upon, but it won't actually affect what I'm doing.

Does anyone see a problem in my code or why it wouldn't be reading the file?


Why not use:

std::string firstword;
inputdata >> firstword; // or std::getline(inputdate, firstword);

while (inputdata >> w >> d)
{
  weight.push_back(w);
  data.push_back(d);
}

It's much cleaner since data and weight go in pairs (maybe I changed w and d).


It is wrong to use .eof() in a while loop. For example,

while(!inputdata.eof()){
   ... 
}

Wrong.

This is the correct use.

if (!(cin >> foo)) {
  if (cin.eof()) {
    cout << "read failed due to EOF\n";
  } else {
    cout << "read failed due to something other than EOF\n";
  }
}

Additionally, at the C++ FAQ, in Section 15.5 it says "Why does my input seem process past the end of a file?"

Because the eof state may not get set until after a read is attempted past the end of file. That is, reading the last byte from a file might not set the eof state. E.g., suppose the input stream is mapped to a keyboard — in that case it's not even theoretically possible for the C++ library to predict whether or not the character that the user just typed will be the last character.

You also need to check if your fstream is properly open by doing this,

if (inputdata.is_open()) { /* ok, proceed with output */ }

Doing if(!inputdata) is not the proper check.


"since there is a one-word title at the beginning of the input file that I need to ignore" That might be problematic. Trying to read a text with ints or floats is asking for trouble.

If this word is on a seperate line, try this: http://www.cplusplus.com/reference/iostream/istream/getline/

Else, try to read the first word with a temporary string.\

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜