files

This is a discussion on files within the C++ Programming forums, part of the General Programming Boards category; Why doesn't this code work? The if statement is always turning out to be true. Code: #include <iostream> #include <fstream> ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    files

    Why doesn't this code work? The if statement is always turning out to be true.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	fstream file;
    
    	char name[33];
    
    	cout << "Enter the name of the file you wish to open" << endl;
    
    	cin.getline(name, 33);
    
    	file.open(name);
    
    	if (file.fail())
    		cout << "Unable to open file" << endl;
    
    	file.close();
    
    	return 0;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Thow a file.clear( ) before the open( ) statement.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    What does the clear() member function specifically do? I'm guessing it clears stuff.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy

    file.clear(); didn't work.

  5. #5
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I have another question about files: when should you use ifstream, ofstream and fstream? I simply use fstream all the time.

  6. #6
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    ifstream is for input operations on files
    ofstream is for output operations.

    i think, thats how i remember it being at least. i havent used streams in a while.

    http://www.cplusplus.com/ref/iostream/ios/fail.html

    is it supposed to fail? try adding this before the if statement to see what happens

    cout << file.fail() << endl;

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    file.open( name, ios::out )
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    cout << file.fail() << endl; made it display 1.

  9. #9
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Thanks XSquared. That worked. I thought adding the mode the file is to be opened is optional, though.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I think it assumes that the file must exist unless you use an ofstream or specify ios:ut.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 07:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 04:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM

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