Reading a binary file into a buffer including nulls

This is a discussion on Reading a binary file into a buffer including nulls within the C Programming forums, part of the General Programming Boards category; I am attempting to read a binary file into a buffer but the file contains nulls (it's a TIFF file). ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    Reading a binary file into a buffer including nulls

    I am attempting to read a binary file into a buffer but the file contains nulls (it's a TIFF file). I need to keep the nulls but the buffer is getting null-terminated as soon as it encounters one. How do I keep the nulls in the buffer and everything that comes after them?

    Code:
    // Read the file into a buffer
    unsigned char *fileBuf;
    FILE *ucmFile = NULL;
    int fileSize, result;
    
    if ((ucmFile = fopen(szFileNamePath, "rb")) == NULL) {
         iRet = 8;
         strcpy(outResponse, "Error: Unable to open ");
         strcat(outResponse, szFileNamePath); 
    } else {
         fileSize = getFileSize(ucmFile);
         fileBuf = (unsigned char*) malloc(sizeof(char)*fileSize);
         result = fread(fileBuf, 1, fileSize, ucmFile);
         if (result != fileSize) {
              iRet = 8;
              strcpy(outResponse, "Error: Unable to read ");
              strcat(outResponse, szFileNamePath); 
         }
         fclose(ucmFile); 
    }
    I didn't show all of the field definitions or the getFileSize method. The fread command is reading into fileBuf, which is defined as an unsigned char*. fileBuf only contains data up until the first null.

    Thanks for your help!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You seem to have posted in the wrong forum as your code is C not C++.

    If you need to manipulate a buffer with nul chars in it in C, you can use the "mem" functions instead of the "str" functions. Look up memcpy and memcmp.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Sorry about the mispost. I will repost in the C forum. I am using VC++ though so if you have a C++ solution, that could work for me. I'm not getting far enough to use mem functions vs str functions because my buffer area is getting null-terminated. The strcpy and strcat functions you see in my code are just to populate status message fields and don't involve my file buffer. I will also try fstream rather than FILE and see if that helps. Thanks!

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It sounds like you're missing the point. The mem functions ignore nuls, whereas the str functions don't. So if you use the mem functions the nuls don't matter.

    Switching to fstream will do nothing.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    You are missing the point. It doesn't matter that you are only using the strxxx function here for actual string stuff. The point is that wherever you have the actual problem (which is not in this code that you have posted), you are using strxxx or printf functions etc.
    There is nothing wrong with the reading of the file itself, although you should probably check that the malloc succeeded.

    Don't repost in the other forums, ask to have this one moved instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Thank you for the reply. I requested that the thread be moved to the C forum. In the meantime, I created a very simple exe using fstream rather than FILE. The only thing this exe does is read a file into a buffer. There are no strxxx functions anywhere. I still have the same problem. The buffer only contains data up until the first null.

    Code:
    int main(int argc, char* argv[])
    {
     string fileBuf;
     ifstream ucmFile("c:\\ucm.tif", ios::in | ios::binary);
     if (ucmFile) {
      ucmFile.seekg(0, ios::end);
      fileBuf.resize(ucmFile.tellg());
      ucmFile.seekg(0, ios::beg);
      ucmFile.read(&fileBuf[0], fileBuf.size());
      ucmFile.close();
     }
     return 0;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,564
    How do you know that the buffer only contains data upto the first nul? How did you view this buffer? If your data contains embedded nul you can't use any of the string functions. You will need to loop through the entire buffer printing it out character by character.

    Jim

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    Oh, I see. I was stepping through the code in Visual Studio and when the buffer was populated, I moused over to see what was in there. I guess it was only showing me up until the null but maybe it really had the whole thing. I will look byte by byte. Thank you!

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Code:
    ucmFile.read(&fileBuf[0], fileBuf.size());
    I'm not sure it is safe thing to do.

    string is not garanted to have continues internal storage.

    you should use vector of chars to make this code portable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-22-2011, 09:24 PM
  2. dump the buffer read from binary file
    By r00t in forum C Programming
    Replies: 23
    Last Post: 11-23-2009, 10:51 PM
  3. Reading a file in binary
    By maxsthekat in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2009, 10:54 AM
  4. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  5. Replies: 7
    Last Post: 04-27-2009, 05:56 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21