fstream, can't find file

I copied that file from this website, but the file can't be found.
I saved the .txt file in the same folder.
I don't know what is wrong...


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

int main () {

fstream filestr;
filestr.open ("test.txt");
if (filestr.is_open())
{
cout << "File successfully open";
filestr.close();
}
else
{
cout << "Error opening file";
}
return 0;
}
Are you getting an error message? Is the file in your current working directory? Are you sure it's named "test.txt"?

Oh and, you want to use ofstream to write, and ifstream to read. Don't use fstream like that, it's most likely your problem.
no error messages, I get the : "Error opening file", it's in the same directory and file name is ok....
It's possible your file name is really "test.txt.txt". If you're using windows, open the folder options and uncheck the box that says "hide extensions of known file types".
Oh and did you use ofstream/ifstream instead of fstream? That could be part of your problem.
it's in the same directory

If you run your program from an IDE the working directory is often not the same as the directory where the executable file is located. Try putting the file in the project directory.
Topic archived. No new replies allowed.