Thread: Copying Binary Files

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Copying Binary Files

    Well I guess I decided to give up on my XOR thing... anyway, I am now writing a program that edits .swf files, just changing the first three characters. I haven't gotten to changing those characters just yet, as I'm having a problem just copying them. They are just binary files, and I should be able to copy them just fine, but when I compare the input and output with UltraCompare, the two have a lot of differences. All that I'm trying to do at the current moment is copy them. Does anyone know why the output the is so different from the input? Here's my code:

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    using std::string;
    string fileName;
    ifstream* openFile()
    {
        for(;;)
        {
            cout << "Enter the name of a file" << endl;
            getline(cin, fileName);
            ifstream* pFileStream = new ifstream((fileName).c_str(), ios::binary);
            if (pFileStream->good())
            {
                return pFileStream;
            }
            cerr << "Couldn't find " << fileName << endl;
        }
    }
    
    int main()
    {
        ifstream* pFileStream = openFile();
        int x = fileName.length();
        int xx = x - 4;
        fileName.insert(xx, "S");
        cout << fileName << endl;
        system("pause");
        ofstream out((fileName).c_str());
        char buffer[80];
        while (!pFileStream->eof() && pFileStream->good())
        {
            pFileStream->read(buffer, 80);
            int noBytes = pFileStream->gcount();
            for(int i = 0; i < noBytes; i++)
            {
                out << buffer[i];
            }
        }
        system("pause");
    }
    Before someone makes a comment about this line of code :
    Code:
    fileName.insert(xx, "S");
    and the preceding two, let me just say that I wanted the original different from the output, it may not be the right way, but it's how I'm doing it. Anyway, any help would be appreciated.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Mike. You are still causing memory leaks with that openfile routine. I would suggest you change it to a reference type and avoid these issues.

    Code:
    ifstream* pFileStream = new ifstream((fileName).c_str(), ios::binary);
    If you fail to open the file, you output that it failed and allocate a new pointer without cleaning up the old one.

    You should clean up on failure

    Code:
    delete pFileStream;
    Just because the file failed to open doesn't mean there isn't memory allocated to the filestream.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, why use a pointer to an ifstream in the first place? You do not need dynamic memory allocation here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by mikeman118 View Post
    All that I'm trying to do at the current moment is copy them. Does anyone know why the output the is so different from the input?
    Code:
    .
    .
    .
            ifstream* pFileStream = new ifstream((fileName).c_str(), ios::binary);
    .
    .
    .
        ofstream out((fileName).c_str()/* ??? */);
    Is there any difference?

    D
    Last edited by Dave Evans; 08-10-2007 at 01:04 PM.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Thank you Dave that solved it! I can't believe I didn't notice that! (Actually, I can) I'll mess with the memory leaks to, but the reason I do it that way is because it's really it's the only way I know how - it's how my book did it, so that's why I do it that way. I'm open to suggestions, though. Thank you.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    There is no need to implement the copy operation using while():

    Code:
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      ifstream i("testfile.swf", ios::binary);  // open input stream
      ofstream o("testfile.new", ios::binary);  // open output stream
      
      i.get(); // throw away character 0..2
      i.get();
      i.get();
      
      o << "ABC" << i.rdbuf(); // insert new header and copy the rest
    
      return 0;
    }

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Oh one more thing that I forgot. The problem is, the beginning of the file in a hex editor is "FWS" (those are the characters I wish to change); so I'd think I'd just access the first thre elements in the string, right? Well, when I try to do so, it gives me other characters in the file. I don't know, however, where to access to get to those characters. Shouldn't they be the first three, being at the front of the file - they are the first three.

  8. #8
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Sorry, I posted and then saw repet's post. Thank you, works perfectly, thanks everyone.

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    This is sort of off topic but still on the same project, so... yeah. I need to use getline, or at least read the entire line, with a multi-dimensional string(I think that's what it's called, e.g. string str[50]). It won't let me use getline with it though, and does anyone know how to, or an equivalent? Thanks


    Edit: Nvm I figured it out, wow that was a stupid mistake.
    Last edited by mikeman118; 08-11-2007 at 08:26 AM.

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Your title is "Copying binary file" yet you feel the need to use getline(), which reads characters, not binary data. The binary functions read() and write() will do what you need. They are in the fstream header file.

    Here's a simple study in copying any file binary or text. Point being, ALL files are binary at heart.

    Code:
    #include <iostream>
    #include <fstream>
    
    int main(int argc, char **argv)
    {
      std::fstream infile, outfile;
      
      infile.open(argv[1], std::ios_base::binary | std::ios_base::in);
      
      if (infile.is_open())
      {
        std::cout << "Opened file.\n";
        outfile.open("copy", std::ios_base::binary | std::ios_base::out);
        char c;
        while (infile.read(&c, 1))
        {
          outfile.write(&c, 1);
        }
        infile.close();
        outfile.close();
      }
      else
      {
        std::cout << "Failed to open " << argv[1] << std::endl;
      }
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary files
    By tuxinator in forum C Programming
    Replies: 3
    Last Post: 12-01-2005, 02:11 AM
  2. Reading Binary files
    By earth_angel in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2005, 06:48 AM
  3. Binary files
    By docetes in forum C Programming
    Replies: 2
    Last Post: 04-21-2005, 01:42 PM
  4. fstream binary files
    By wesdgreat in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 10:12 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM