Thread: Binary file I/O

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Binary file I/O

    Why doesn't this program make a carbon copy? The output file is smaller than its counterpart and does not function.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        unsigned long int x = 0;
        unsigned char buffer;
        ifstream reader("C:\\windows\\desktop\\1.jpg", ios::in | ios::binary);
        ofstream writer("C:\\windows\\desktop\\1_out.jpg", ios::out | ios::binary);
        if(reader.fail())
        {cout << "Failed to open file" << endl; return 1;}
        if(writer.fail())
        {cout << "Failed to open write stream" << endl; return 1;}
        
        while(!reader.eof())
        {
         reader >> buffer;
         if(x < 100) cout << buffer;
         writer << buffer;
         x++;
        }
        cout << x << endl;
        reader.close();
        writer.close();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while(!reader.eof())
    That's one of your problems. The FAQ will explain why.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I think you need to be using the read and write function, because you are doing binary (i.e. unformatted) I/O. So, I suggest:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        char buffer;
        ifstream reader("file1.jpg", ios::in | ios::binary);
        ofstream writer("file2.jpg", ios::out | ios::binary);
        if(reader.fail()) {
            cout << "Failed to open file" << endl; 
            return 1;
        }
        if(writer.fail()) {
            cout << "Failed to open write stream" << endl; 
            return 1;
        }
        
        while(!reader.eof()) {
            reader.read(&buffer,1);
            writer.write(&buffer,1);
        }
        reader.close();
        writer.close();
        
        return 0;
    }
    
    This seemed to work for me, except for the one small problem that the resulting file is one byte larger than the initial file. Maybe someone else can spot this other problem?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I see. Then is there some way to recognize when the istream encounters no more data; other than by using EOF (which I just read doesn't really work the way the name implies)?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Like this:
    Code:
    while(reader.read(&buffer,1))
    {
      //stuff
    }
    Woop?

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Ok...so I posted my solution, and then I read Prelude's post, and I thought "I didn't know that. Let me look at the FAQ and see what this is all about." So I went to the FAQ and I used the search bar at the bottom and I searched on "eof()". I got one hit, for a very long thread about some text based game or something, and I thought, "If the answer is buried in there somewhere, then forget it." So clearly I have no idea how to search the FAQ and make good use of it. Is there a FAQ for the FAQ?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You know, the confusion I had was that whenever I hear people talking abou the FAQ, I always think of the FAQ message board. I didn't realize that there was FAQ pages on the site. And the FAQ explains why my code produces an output file that is one byte larger.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You know, the confusion I had was that whenever I hear people talking abou the FAQ, I always think of the FAQ message board. I didn't realize that there was FAQ pages on the site. And the FAQ explains why my code produces an output file that is one byte larger.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There's a simpler way to do the copying:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        ifstream reader("C:\\windows\\desktop\\1.jpg", ios::in | ios::binary);
        ofstream writer("C:\\windows\\desktop\\1_out.jpg", ios::out | ios::binary);
    
        if(reader.fail())
        {cout << "Failed to open file" << endl; return 1;}
        if(writer.fail())
        {cout << "Failed to open write stream" << endl; return 1;}
        
        // Copies entire contents of reader stream into writer stream
        writer << reader.rdbuf();
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Question writer << reader.rdbuf(); What next?

    Quote Originally Posted by hk_mp5kpdw
    Code:
    [/
    ifstream reader("C:\\windows\\desktop\\1.jpg", ios::in | ios::binary);
        ofstream writer("C:\\windows\\desktop\\1_out.jpg", ios::out | ios::binary);
    
        if(reader.fail())
        {cout << "Failed to open file" << endl; return 1;}
        if(writer.fail())
        {cout << "Failed to open write stream" << endl; return 1;}
        
        // Copies entire contents of reader stream into writer stream
        writer << reader.rdbuf();
    Thanks for this - this has helped me out!

    I am trying to create my own "file format". My application can save two bitmaps into one file, with some "ini" settings at the top of the file e.g.:

    [BEGININI]
    COLOR=RED;
    TEXT='SaveMyText'
    [ENDINI]
    [FILE1]
    BMP BINARY CONTENTS HERE
    [ENDFILE1]
    BMP BINARY CONTENTS HERE
    [FILE2]
    [ENDFILE2]

    I used your code above to create this file but do you know how I could parse the images back and display them?

    Any help would be appreciated - thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Binary FIle I/O
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 03-21-2003, 02:22 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. I/O to binary file
    By pors7 in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2001, 02:27 PM