Thread: Problem reading files

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Problem reading files

    Code:
    infile.open(input)			
    if(!infile)			
    	{							
    	cout<<"Invalid file name.  Try again."<<endl;
    	}
    Here, I am trying to read the file designated by "input".
    Infile is an instance of the ifstream class.

    When I enter a valid filename, everything works fine, but if I enter an invalid file name, the program reprompts to enter a filename, and all subsequent attempts fail to open the file, even if the input is cleared and the file name is correct. I know for SURE that the problem is in this section of code; I do not know how to remedy it though.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    failflag is set. infile.clear() resets error flags.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    infile.clear();

    Do this before trying to reopen the file.

    Meh - 2nd time today I've been beaten ...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Ah, thanks guys. Works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  2. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 04:41 PM
  3. Replies: 0
    Last Post: 07-12-2002, 01:40 PM
  4. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM