need help with file opening errors

This is a discussion on need help with file opening errors within the C++ Programming forums, part of the General Programming Boards category; hi! i have a question about file opening. i'm supposed to allow a user to enter filenames of 5 text ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    need help with file opening errors

    hi! i have a question about file opening. i'm supposed to allow a user to enter filenames of 5 text files. here is a portion of my code.
    Code:
    do {
    	cout << "Enter filename(e.g abc.txt): ";
    	cin >> filename;
    	inFile.open(filename.c_str());
    
                    if(inFile.fail()) {
    		cout << "Error!\n";
    	}
    		
    	else {
    		
    	inFile >> f;
                    ..so on...
                    file_count++;
    	}
    	inFile.close();
    		
    }while(file_count < 5);
    it works fine if the file can be found everytime.
    i thought i have closed my file after each round so it doesnt matter. but after typing an incorrect filename once the program will state that inFile failed even though i typed a valid filename.
    anyone knows whats wrong with this?

    also, i changed the extension of another file type to .txt and tried opening it within the program. this causes the program to hang. just wondering is there's a way to test if the file is really a .txt before opening it. my aim is for the program to handle all invalid files properly..

    i thank all who are willing to help or even spend time reading about my problem. =)

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    There is a tutorial written by someone on the forums (whose name I can't remember, but it begins with a), and in it there's a search algorithm, for searching for files in a folder or whatever. I had it on my mem-stick, but that died, so I can't give it to you. On the other hand, I don't think there's any reason for that to fail. This is what I use for getting file info:

    Code:
    bool GetFileInfo ( string FileName, string &FileInfo )
    {
    
        ifstream in ( FileName.c_str() );
    
        if ( in )
        {
            string Temp;
            FileInfo.clear();
    
            while ( getline( in, Temp ) )
            {
                FileInfo += Temp;
                FileInfo += "\n";
            }
    
            in.close();
        }
        else
        {
            in.close();
            return false;
        }
    
        in.close();
    
        return true;
    }
    it's a function, so you could use it like this:

    Code:
    string FileInfo;
    if ( GetFileInfo( "MyFile.txt", FileInfo ) )
    {
    	cout	<< endl << "File exists!! Here are the contents of the file:"
    		<< '\n' << FileInfo;
    }
    else cout<< "File access denied!";
    and put the function before main(). Because it's a bool, it works nicely with if() statements etc.

    EDIT:I found the site. Just search through it, and the searching thing should be there somewhere, and if you manipulate it, you should be able to use it in yuour program.
    Last edited by twomers; 07-13-2006 at 05:27 AM.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    thank you very much twomers for looking into my problem.

    looking at your GetFileInfo function i realised that you closed the file 3 times, i thought that just closing it once after the if and else would do. nevertheless i tried doing that and it didnt work.

    next i tried putting it into a function as well, (my if(inFile) check was in main) and it was no good too.

    i suspect that the problem comes in when i loop the whole thing. it seems that after inFile is messed up once, it stays permanently in failed state. even closing the file doesnt help.

    still, thank you very much for your reply.

    also, just to clarify my second question. what i had intended was not to search the folder for valid files. but rather to "peek" a little into the content of the file to verify that if is really a .txt file so that the program does not hang even if i opened for example abc.txt, which was initially abc.gif

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    On your second point - I know what you mean, but if you search the folder for text files, *.txt, you can compare a list of them with what you entered. That way it won't 'hang', as you say it does. I don't think it should hang though.

    Oh, and you're right, I did close it three times. I should have only done it twice, and gotten rid of the close() in the if() statement, or the one at the very end.

    If I compile and run this:

    Code:
    int main( void )
    {
    	Matrix thing;
    	
    	string Info = "";
    
    	bool OpenOK = GetFileInfo( "thing.txt", Info );
    
    	cout<< OpenOK;
    
    	return 0;
    }
    and the file thing.txt exists, it works fine, and OpenOK = 1. However, if I change it to thing.txtt, or if I delete the file thing.txt, OpenOK is 0. It works fine for me.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    thank you very much twomers! it works now! the first part is now solved =)

    just curious about the 2nd part, sorry this might sound really complicated due to my poor explanation, basically, does findfirst simply compare the filename as we see in windows explorer or is it smart enough to know that the txt file is actually gif so it returns false for abc.txt. abc.txt is there, only that it is a fake(gif file with changed extension)

  6. #6
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Hi, about the failure state, call the method clear if it is your intention to open a file in the same file variable. There are three flags in the file that are set according to what happens, they are accessed by the following methods:

    - fail() - error reading different types int=string for example
    - bad() - other errors and the above
    - eof() - end of file

    Code:
    my_file.open(a_name);
    
    // teste here
    
    //use the file here if good
    
    my_file.cose();
    my_file.clear();
    Last edited by Mortissus; 07-13-2006 at 07:11 AM.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    hey thanks Mortissus! that was a much shorter way of solving the problem lol. i tried using it before but i always put it before .close() so it never worked.

    thanks to all who helped =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. ifstream in opening a file
    By vin_pll in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2008, 03:17 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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