How does it happens:cannot read file?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 7
Reputation: gshock2006 is an unknown quantity at this point 
Solved Threads: 0
gshock2006 gshock2006 is offline Offline
Newbie Poster

How does it happens:cannot read file?

 
0
  #1
Feb 4th, 2009
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!
  1.  
  2. #include<iostream>
  3. #include<fstream>
  4. #include<string>
  5. #include<iomanip>
  6. #include<cstdlib>
  7. #include<vector>
  8. using namespace std;
  9. //using namespace stdext;
  10.  
  11. int main()
  12. {
  13. ifstream inClientFile("Dic.dat",ios::in);
  14.  
  15. if(!inClientFile)
  16. {
  17. cerr<<"File could not be opened"<<endl;
  18. exit(1);
  19. }
  20. ...

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".
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: galin is an unknown quantity at this point 
Solved Threads: 2
galin galin is offline Offline
Newbie Poster

Re: How does it happens:cannot read file?

 
0
  #2
Feb 4th, 2009
program can not find the file. Maybe program and file are not in the same directory . Put the full path to the file
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,915
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: How does it happens:cannot read file?

 
0
  #3
Feb 4th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How does it happens:cannot read file?

 
1
  #4
Feb 4th, 2009
  1. if(!inClientFile)
Please do not use this method, it is so wrong. You would be better using the following
  1. if(!inClientFile.good())
Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: gshock2006 is an unknown quantity at this point 
Solved Threads: 0
gshock2006 gshock2006 is offline Offline
Newbie Poster

Re: How does it happens:cannot read file?

 
0
  #5
Feb 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,915
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 304
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: How does it happens:cannot read file?

 
0
  #6
Feb 4th, 2009
How "doesn't it work" ? Does it compile?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: gshock2006 is an unknown quantity at this point 
Solved Threads: 0
gshock2006 gshock2006 is offline Offline
Newbie Poster

Re: How does it happens:cannot read file?

 
0
  #7
Feb 4th, 2009
it can be complied, but just display the message which means it cannot find the dat file,i suppose.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How does it happens:cannot read file?

 
0
  #8
Feb 4th, 2009
Originally Posted by gshock2006 View Post
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: gshock2006 is an unknown quantity at this point 
Solved Threads: 0
gshock2006 gshock2006 is offline Offline
Newbie Poster

Re: How does it happens:cannot read file?

 
0
  #9
Feb 4th, 2009
Originally Posted by Freaky_Chris View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: How does it happens:cannot read file?

 
0
  #10
Feb 4th, 2009
It worked for me without any problems.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC