Hi,guys,
I am new to C++,and recently I practice to read a file with visual studio 9.0,but it turns out that it cannot find the very file!

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<vector>
using namespace std;
//using namespace stdext;

int main()
{
	ifstream inClientFile("Dic.dat",ios::in);

	if(!inClientFile)
	{
		cerr<<"File could not be opened"<<endl;
		exit(1);
	}
...

I ensure that the file "Dic.dat" do exist in the same file of the project,but the program only display the message "File could not be opened".

program can not find the file. Maybe program and file are not in the same directory . Put the full path to the file

if the file "Dic.dat" is in the same directory as your source-files, your program should work fine. Are you sure that you didn't make a typo?

if(!inClientFile)

Please do not use this method, it is so wrong. You would be better using the following

if(!inClientFile.good())

Chris

commented: Holy s***, I completely missed that :( +12

I am sure the "dic.dat" is in the same directory with the source file,
moreover, I tried the expression
if(!inClientFile.good())
but it didn't seem to work.

How "doesn't it work" ? Does it compile?

it can be complied, but just display the message which means it cannot find the dat file,i suppose.

I am sure the "dic.dat" is in the same directory with the source file,
moreover, I tried the expression
if(!inClientFile.good())
but it didn't seem to work.

Lets say you are compiling with VC++ then your source code isn't in the same location as the debug or release exectuable, which means nore will "Dict.dat" be, this means you will need to move your Dic.dat file into the debug/release folders.

Chris

Lets say you are compiling with VC++ then your source code isn't in the same location as the debug or release exectuable, which means nore will "Dict.dat" be, this means you will need to move your Dic.dat file into the debug/release folders.

Chris

I tried that,but it didn't work,either. I also have to specify the very path of the dic.dat, it failed ,too.

It worked for me without any problems.

Yup. Are you [sure] that it's not a typo? For exampe dic.dat vs dict.dat?

thanks for all your help,finally the problem has been solved partly,namely the exact problem of the source code lies on the other part of the program which I mistook for this part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.