944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1502
  • C++ RSS
Feb 13th, 2006
0

problem opening files

Expand Post »
Hey guys I am using code that does the following:

C++ Syntax (Toggle Plain Text)
  1. cin.ignore();
  2. cin.getline(file, 250);
  3.  
  4. ifstream test;
  5.  
  6. test.open (file);
  7.  
  8. if (test.fail())
  9. {
  10. cout<<"error"<<endl;
  11.  
  12. exit(1);
  13. }
  14.  
  15. ...

The problem is that if a file name is entered that does not exist for example c:\\fred.txt the error message will not appear and the txt file fread will be created, but if I enter "c:\\fred.txt" the code seems to work, but I have created similar code before using cin.getline and the quotes were not needed. Any ideas what I have done wrong?

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Feb 13th, 2006
0

Re: problem opening files

when you enter the filename from the keyboard do not use double backslash -- double slashes are only needed for string literals in your program. So you should type c:\fred.txt , only one backslash. Reason: the compiler does not interpret anything you enter from the keyboard, it only interprets string literals.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 13th, 2006
0

Re: problem opening files

Hi thanks for your reply but when i do this I still have the same problem.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Feb 13th, 2006
0

Re: problem opening files

then the problem is elsewhere. try test.is_open() instead of if test.fail(). Use your compiler's debugger and look at what exactly is in the file buffer, or use cout to display it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 13th, 2006
0

Re: problem opening files

Quote originally posted by Ancient Dragon ...
then the problem is elsewhere. try test.is_open() instead of if test.fail(). Use your compiler's debugger and look at what exactly is in the file buffer, or use cout to display it.
Hi thanks again for your reply, when i use test.is_open() it doesn't find any file even if it exists.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005
Feb 13th, 2006
0

Re: problem opening files

I have managed to solve the problem by using ios::nocreate, but as I used ifstream I thought I shouldn't have to use this, any ideas? Thanks.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Jon182 is offline Offline
91 posts
since Jul 2005

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: errors points to excpt.h
Next Thread in C++ Forum Timeline: Getting all data from an input and output file





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


Follow us on Twitter


© 2011 DaniWeb® LLC