• 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.
  • Posts

  • Recent Achievements

    • One Year In
      Console General earned a badge
      One Year In
    • One Year In
      Twozo Technologies earned a badge
      One Year In
    • One Month Later
      Twozo Technologies earned a badge
      One Month Later
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
    • Veteran
      branfont went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      510
    2. 2
      +Edouard
      200
    3. 3
      PsYcHoKiLLa
      131
    4. 4
      Steven P.
      89
    5. 5
      neufuse
      74
  • Tell a friend

    Love Neowin? Tell a friend!