photon photon - 1 year ago 161
C++ Question

ifstream won't open file

I'm trying to open a file so I can read from it.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

ifstream input_file("blah.txt", ios::in);
ofstream output_file("output.txt", ios::out);

cerr << "Error" << endl;
cout << "good 2 go" << endl;

This is the code I have for reading the file named blah.txt and the output I'm getting at the terminal is the "Error". I am using Linux Mint 14 and gVim, so when I enter the :pwd command, I know I am in directory /mnt/share. Checking from the terminal, file blah.txt is in the same directory. The only thing I can think of is hidden file extensions. Why can't I open the file?

Answer Source

That's because you open "blah.txt" twice.

First time:

ifstream input_file("blah.txt", ios::in)

Second time:"blah.txt")

Removing the second one should fix your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download