943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1663
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 4th, 2009
0

How does it happens:cannot read file?

Expand Post »
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!
C++ Syntax (Toggle Plain Text)
  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".
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gshock2006 is offline Offline
7 posts
since Feb 2009
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

program can not find the file. Maybe program and file are not in the same directory . Put the full path to the file
Reputation Points: 10
Solved Threads: 2
Newbie Poster
galin is offline Offline
10 posts
since Aug 2007
Feb 4th, 2009
0

Re: How does it happens:cannot read 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?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 4th, 2009
1

Re: How does it happens:cannot read file?

C++ Syntax (Toggle Plain Text)
  1. if(!inClientFile)
Please do not use this method, it is so wrong. You would be better using the following
C++ Syntax (Toggle Plain Text)
  1. if(!inClientFile.good())
Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gshock2006 is offline Offline
7 posts
since Feb 2009
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

How "doesn't it work" ? Does it compile?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

it can be complied, but just display the message which means it cannot find the dat file,i suppose.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gshock2006 is offline Offline
7 posts
since Feb 2009
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

Click to Expand / Collapse  Quote originally posted by gshock2006 ...
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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gshock2006 is offline Offline
7 posts
since Feb 2009
Feb 4th, 2009
0

Re: How does it happens:cannot read file?

It worked for me without any problems.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dont understand the error in this code
Next Thread in C++ Forum Timeline: cannot connect sql server in c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC