Thread: ifstream problem

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    ifstream problem

    Code:
    sprintf (fileName, "%s%d.sss", path, fileNo);
    
    		inFile.open (fileName, ios::in);
    
    		if (!inFile)
    			cout << "error" << endl;
    
    		else
    		{
    			cout << "Successfully opened file " << fileName;
    			inFile.close ();
    		}
    Non-existent files don't flag the error! What on earth?!

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I've changed the code so it uses FILE * now.
    And the problem still exists.
    According to this program, every conceivable filename exists on my humble little hard disk. Quite an achievment. Anyone want a file? Because according to my PC I possess every single file in the universe...

    [/sarcasm]

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by samGwilliam
    Non-existent files don't flag the error! What on earth?!
    Sure they do.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    int main(void)
    {
    	std::string filename = "nothere.txt";
    	std::ifstream infile;
    	infile.open(filename.c_str(),std::ios::in);
    	if (!infile)
    		std::cout << "Error" << std::endl;
    	else
    		std::cout << "No error" << std::endl;
    
    	return 0;
    }
    Code:
    Output:
    Error
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    On mine it doesn't. Unless I use a string literal:

    Code:
    inFile = fopen ("..............", "r");
    It clearly says that doesn't exist. The other technique, however, will insist files that don't exist, do.

    I JUST DISCOVERED IT HAS, HOWEVER, EITHER FILES, OR WIPED THE EXISTENNT ONES CLEAN. GOOD JOB I BACKED THEM UP!!!

  5. #5
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Sorry about that. I've discovered the problem. I failed to use ios::nocreate. By the time I switched over to FILE* = file = fopen (etc), the files had already been created.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'll bet you're using MSVC++ 6.0. I don't think nocreate is a standard flag. 6.0 was pretty terrible at conforming to ANSI. I'd say get a new compiler.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by pianorain
    I'll bet you're using MSVC++ 6.0. I don't think nocreate is a standard flag. 6.0 was pretty terrible at conforming to ANSI. I'd say get a new compiler.
    I am. That bad, eh?

    Bollox.

    Anyhow I got it working now. The program is a file to stitch all my reclaimed sectors together (I had to do a low level search and save them one by one - it took hours). It's working a treat - I can see all my beloved C projects I thought I'd lost coming back together. It's nice - it concatenates all consecutive filenames into single files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM