• 0

[C++] can't open files


Question

I have no idea why the following code doesn't open the file "blueprint.txt" (which is in the same directory as the source code). The code compiles fine, but returns the error message. Ive tried changing the code to

ifstream file("blueprint.txt");

OR

ifstream file;

file.open("blueprint.txt", ios::in);

OR

ifstream file;

file.open("blueprint.txt");

All to no avail. This is a simple process and I'm at a loss as to why its giving me trouble.

#include <iostream>

#include <fstream>

using namespace std;

int main() {

ifstream file("blueprint.txt", ios::in);

if (!file.good()) {

cerr << "Can't open file " << endl;

} else {

cout << "File opened." << endl;

}

return 0;

}

Thanks in advance.

Link to comment
https://www.neowin.net/forum/topic/681360-c-cant-open-files/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

int main() {

	ifstream file("blueprint.txt", ios::in);

	if (!file.good()) {
		cerr &lt;&lt; "Can't open file " &lt;&lt; endl;
	} else {
		cout &lt;&lt; "File opened." &lt;&lt; endl; 
	}

	return 0;
}

Runs fine for me. Post the error you get.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.