954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How does it happens:cannot read file?

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".

gshock2006
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

galin
Newbie Poster
10 posts since Aug 2007
Reputation Points: 10
Solved Threads: 2
 

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?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
if(!inClientFile)


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

if(!inClientFile.good())


Chris

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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.

gshock2006
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

gshock2006
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 
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

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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.

gshock2006
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

It worked for me without any problems.

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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.

gshock2006
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You