reading a file problem

This is a discussion on reading a file problem within the C++ Programming forums, part of the General Programming Boards category; Hey guys I am using code that does the following: Code: cin.ignore(); cin.getline(file, 250); ifstream test; test.open (file); if (test.fail()) ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    reading a file problem

    Hey guys I am using code that does the following:

    Code:
    cin.ignore(); 
    cin.getline(file, 250);
    
    ifstream test;
    
      test.open (file);
    
    	if (test.fail())
    		{
    		  cout<<"error"<<endl;
    
    		  exit(1);
    	     }
    
    ...
    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. Any ideas what I have done wrong?

    Thanks.
    Last edited by 182; 02-12-2006 at 07:47 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could use:
    Code:
      test.open (file);
    
      if (!test.is_open())
      {
        cout << "Unable to open file: " << file << endl;
        return 1;
      }

  3. #3
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi thanks for the reply, but do you know why my code isn't working?
    Last edited by 182; 02-13-2006 at 09:26 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your code works fine for me: if I enter a filename that doesn't exist, I get the error message, and if I enter a filename that does exist, no error message is displayed.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    What about the nocreate flag?

    Code:
    test.open (file, ios::nocreate);
    Sent from my iPadŽ

  6. #6
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hey thanks for your reply the ios::nocreate has worked but I am just wondering what this code does and how come I needed it as I wasn't telling the program to create the file?

    Thanks
    Last edited by 182; 02-13-2006 at 04:30 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Be default, open() will open a file if it exists or create it if it doesn't. Hence the reason they made the nocreate flag. Why it worked for 7stud, I don't know. It's probably a compiler to compiler thing. Different versions of fstream or what have you.
    Sent from my iPadŽ

  8. #8
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks, Does this also mean that I will need to use the ios::noreplace code as well?

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    It depends. Are you trying not to replace the file if it's already there? In that case you might as well just use ios::app since it will fail to open if it doesn't exist anyway.

    Alternetavely. If you wanted to use nocreate or noreplace, you should read up on this. Both of those flags are obsolete and therefor may not work with all compilers or OSs.
    Last edited by SlyMaelstrom; 02-13-2006 at 04:35 PM.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Be default, open() will open a file if it exists or create it if it doesn't.
    Since 182 used an ifstream, the default should be to not create the file. Of course if somehow an ofstream or fstream was accidently used, then well ...
    Code:
    ifstream test;

  11. #11
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Im not trying to replace the file, just open it and read its contents but as I needed the nocreate so the file wouln't be created will I need the noreplace so that the file will not be replaced?

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Hmm... is that true? I thought that was default for both ifstream and ofstream. Oh well, I guess it makes sense not to create an empty file to try and read from.
    Sent from my iPadŽ

  13. #13
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by swoopy
    >Be default, open() will open a file if it exists or create it if it doesn't.
    Since 182 used an ifstream, the default should be to not create the file. Of course if somehow an ofstream or fstream was accidently used, then well ...
    Code:
    ifstream test;
    Hi I am using ifstream but I am using the fstream.h header file does this make a difference?

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    fstream.h is depreciated. It could likely be the reason your functionality is different than 7stud's. You should probably use <fstream> instead.
    Sent from my iPadŽ

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    when they mean fstream/ifstream/ofstream they are talking about the command. nocreate isn't needed because the ifstream can't create files.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21