Error reading C++ file

Asked

Viewed 436 times

0

Can anyone tell me why the output of this program is just "end"? The file naocompre.txt is in the same directory as the program.

Follows the code:

#include<iostream>
#include<fstream>
#include<string>

using namespace std;


int main ()
{
string info;
ifstream input("naocompre.txt");
if(input.is_open()) {
    while(!input.eof()) {
           string info;
           getline(input,info);
           cout<<info<<endl;
    }
}
    cout<<"end"<<"\n";
    return 0;

}

Contents of naocompre.txt:

NaoCompre Costeira 
5 
7 
4 
Maria_Benta 1 800 
Juliana_Digito 1 800 
Zeca_Mole 3 180 
Joao_DeMora 3 180 
  • Would need thresh. See where it is running, where it is jumping, isolate the problem until it reaches the point that indicates the problem. He may not have been able to open the file, or it could be something with the data. Except for not closing the file, which is not a problem in something so simple, I’m not seeing problems in the code. It may have escaped me.

  • 2

    The code doesn’t enter the if(input.is_open()) { ... } i.e., the file was not found or cannot be opened (it is not open in another program?).

1 answer

0


The program is correct. The problem is in your environment. As commented by @stderr, either the file doesn’t exist, or is in another directory, or has another name, or something like that.

P.S. " In my machine it worked."

  • Actually, compiling by the terminal outside the eclipse works normally, but why does Eclipse not work?

  • 1

    Solved. To run in eclipse it was necessary to specify the full path of the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.