Thread: ofstream - strange exit from program

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    25

    ofstream - strange exit from program

    Hey, I'm currently looking into steganography and I decided to create a C++ program to hide a txt file in a bmp file as a little learning experience. All this code runs fine, my problem is occurs when saving the fixed BMP data to file. I'm using the MinGW compiler with msys. I put some text to help with debugging:

    Code:
    std::cout << "Processing data";
    
    //Function calls to process data go here (i won't go into it.)
    
    std::cout << "Done!" << std::endl;
    
    std::cout << "-----------------------------------------" << std::endl;
    
    std::cout << "Saving to file...";
    
    OutToFile(); //This function saves the data to a new bmp file.
    
    std::cout << "Done!" << std::endl;
    When I run the program, my output is this ($ indicates prompt)

    $ ./steg file.txt file.bmp

    Processing Data...Done!
    ----------------------------------------------
    Saving to file...

    $

    The program just ends. No stackdump or anything. Here is the OutToFile function:

    Code:
    void Steg::OutToFile() const{
    
    	ofstream f("final.bmp", ios::binary | ios::trunc); //Filestream for saving
    
    	//test for failure (Exception class exists)
    	if(!f) { throw Hiding::Exception(); }
    	
    	f.write(finalBMP.c_str(), finalBMP.size());
    
    	f.close();
    	std::cout << "Done!" << std::endl;
    
    }
    The exception is not thrown. (the try and catch are located in my main() function)

    I tried removing everything from the function and placing this in it.

    Code:
    std::cout << "Testing" << std::endl;
    The program then ran correctly, so I added this.

    Code:
    ofstream f("final.bmp", ios::binary | ios::trunc); //Filestream for saving
    And the program failed again. Does anyone have a clue what's happening? I have included the correct headers and using directives because the ifstreams at the start of the program work fine. It's simply when I open this new ofstream that the program just stops in it's tracks and exits.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    if(!f)
    {
        cerr << "Fail!/n";
        throw Hiding::Exception();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Strange errors in linked list program
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 01-02-2006, 08:36 AM
  4. Cannot get my program to exit properly (switch related)
    By Skarjak in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 10:14 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM