HAVING A PROBLEM OPENING THE INPUT FILE

I'm still learning C++ and i'm currently reading about file I/O. According to the book i'm using, you need to open your input file as follows:

ifstream in_stream;

in_stream.open("MyFile.dat");
if(in_stream.fail())
{
cout << "File opening failed" <<endl;
exit(1);
}

So what i did before writing the program was to creat the file MyFile on my desktop with my input data in it. THE PROBLEM HERE IS THAT MY PROGRAM DOESN'T SEEM TO FIND THIS FILE, i don't know if its because of the location of the file. please help me.
you have to give the complete path of the file in in_stream.open()
closed account (S6k9GNh0)
It's relative to the directory the executable was executed in. Place the file in the directory you start the executable in.
Last edited on
The file must be in the Current Directory (which is the directory of your project). Try this, move the file to, say,
drive D, the in_stream.open("D:\MyFile.dat") must be successful. If the operation is successful, then as I said earlier, you need to put your file in the Current Directory, if the operation fails, then its probably due to wrong extension. Check if your MyFile.dat actualy is MyFile.dat (with dat extension) or if you created it in notepad, it could be MyFile.dat.txt. This too can cause problems
you can also change the directory to the directory where the path is by
SetCurrentDirectory( ) function .
Thank you guys, using the complete directory did help. I have one more question though, IS IT POSSIBLE TO READ YOUR INPUT FROM AN MS WORD FILE?
opening the .doc now working for me but i guess it should be working .
closed account (S6k9GNh0)
............................................
.doc files are encoded in Micro$oft's proprietary format. You can't read ascii characters from that like a normal text file.
Topic archived. No new replies allowed.