Thread: binary file

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    13

    binary file

    When using read() to read a binary file's data, it only gets the first byte, like: ÿû’D

    Does anyone know how I might be able to fix this?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    That's more than a byte. May we see some code?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Code:
    filename = "encrypter.exe";
    
    ifstream file(filename, ios::in|ios::binary|ios::ate);
        
    if(file.is_open())
    {
           char* memblock = new char[filesize];
           int filesize = thefile.tellg();
    
           file.seekg(0, ios::beg);
           file.read(memblock, filesize);
           file.close();
    }

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
           char* memblock = new char[filesize];
           int filesize = thefile.tellg();
    Woopsies.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    that isn't the problem. that mistake was made while posting the code(filesize is declared and value is set before memblock).

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    May I see the code that is sitting in your IDE or text editor at this very moment which is causing troubles.

  7. #7

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    send(mysocket, filesize_char, 256, 0);
    This sends 256 bytes which is more then your filesize_char variable. You can use strlen() to get the correct size.

    Also if you want to send the filesize as text then the receiver needs a way of knowing when the filesize ends and the file data begins such as a newline charactor.

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        filename = new char[256];
        filename = "encrypt.exe";
    Code:
        filename = new char[256];
        strcpy(filename, "encrypt.exe");
    And then delete[] the allocated memory somewhere. Or don't allocate it like that.

    Code:
    send(mysocket, filesize_char, 256, 0); // send the file name
    It's only 50 bytes. And it may be unintended, that's not the filename.

    Use only one filehandle, thefile or file.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    thanks, but that stuff isn't the problem. I can succesfully open a text file, and send it. But when trying to open a bin file(like "encrypt.exe"), it only reads a small ammount.

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    How do you know that it isn't reading the whole file? For example:
    Code:
    file.read(memblock, filesize);
    file.close();
    std::cout<<memblock; //memblock isn't null-terminated!
    //you'd have to do this instead:
    for (int i=0;i<filesize;i++)
      std::cout<<memblock[i];
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    When the buffer reaches the other end, I write the buffer to file. And only a small ammount of the original file is sent and written.

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Looking at your code, I don't see why you open the file twice with two separate ifstreams (it's especially confusing since you use similar names for both of them). I don't really think that's the problem though.

    How do you really know that all the data is sent? You don't even check the return value of send(). Also, have you checked the other data that is sent to be sure it's correct (e.g., the file length)? And how exactly are you writing to the file on the other end (e.g., using the write() member function)?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    The problem is not with the other end, the problem is with read(). The buffer without being sent over sockets, still is only like 3 bytes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 12:43 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM