Saturn
Saturn

Reputation: 18149

Why can't I read a file with Code::Blocks C++?

I've created a file in Code::Blocks called datos.csv, and I have this code:

std::ifstream file("datos.csv");
if (file) {
    cout << "Managed to read file successfully.";
}else{
    cout << "Unable to read file.";
}

But it is unable to read the file.

I tested the same code with TextMate, which can run C++ files, and it was indeed able to read the file, so I suppose there's something up with Code::Blocks. What am I missing?

My file appears listed in "Others" in Code::Blocks' navigator.

Upvotes: 2

Views: 16111

Answers (2)

Federico Ramos
Federico Ramos

Reputation: 51

you need to modify the Target Properties, go to Project -> Properties -> Build targets and change the "Executing Working Dir" for the debug/release folder of your proyect, I hope this help.

Greetings. Saludos.

Upvotes: 4

Captain Obvlious
Captain Obvlious

Reputation: 20073

It can't find the file to open it. Since you are not using absolute paths to open the file it must be relative to the current working directory. If you are launching from the debugger you can set the working directory used when the application is launched. Make sure that directory is the same as where the csv file is located.

Upvotes: 1

Related Questions