Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading 'null' from binary files into char arrays/strings

reading 'null' from binary files into char arrays/strings

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncsharpvisual-studiodata-structures
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dfn

    I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!

    C Offline
    C Offline
    Chris Losinger
    wrote on last edited by
    #2

    dfn wrote:

    I assume this is so because one of the bytes I read (00) is treated like '\0' (null)

    it's not just "treated like" a null, it is a null. C-style strings end on zero bytes. short answer: if your data contains 0's, don't treat it as a C string. long answer: if you need to read non-text data, you're going to have to read the data into byte arrays (char/u-char) and manipulate it there, with pointers - don't put it in std::strings or CStrings or anything like that because those objects are going to see those 0's and treat them as string terminators.

    image processing toolkits | batch image processing | blogging

    D 1 Reply Last reply
    0
    • D dfn

      I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #3

      Are you using Unicode? If so, can you use wstring instead of string?


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      D 1 Reply Last reply
      0
      • C Chris Losinger

        dfn wrote:

        I assume this is so because one of the bytes I read (00) is treated like '\0' (null)

        it's not just "treated like" a null, it is a null. C-style strings end on zero bytes. short answer: if your data contains 0's, don't treat it as a C string. long answer: if you need to read non-text data, you're going to have to read the data into byte arrays (char/u-char) and manipulate it there, with pointers - don't put it in std::strings or CStrings or anything like that because those objects are going to see those 0's and treat them as string terminators.

        image processing toolkits | batch image processing | blogging

        D Offline
        D Offline
        dfn
        wrote on last edited by
        #4

        Thanks for your help! I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string). I couldn't get unsigned chars to work with read(). Every attempt I made, trying to work with chars & strings, or chars only, had the same result.

        C 1 Reply Last reply
        0
        • D David Crow

          Are you using Unicode? If so, can you use wstring instead of string?


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          D Offline
          D Offline
          dfn
          wrote on last edited by
          #5

          Thanks for the reply. Not using Unicode, the file is ANSI-encoded.

          1 Reply Last reply
          0
          • D dfn

            Thanks for your help! I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string). I couldn't get unsigned chars to work with read(). Every attempt I made, trying to work with chars & strings, or chars only, had the same result.

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #6

            dfn wrote:

            I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string).

            right, because binary data is not a string (strcat = string-concatenate). you are not going to be able to use any function or object that manipulates C-style strings - no "str*" functions, no "string" objects, no *scanf, or *printf functions.

            image processing toolkits | batch image processing | blogging

            D 1 Reply Last reply
            0
            • C Chris Losinger

              dfn wrote:

              I tried strcat on "lr" and "rec" (char arrays), but that resulted in the same thing as adding them to "data" (string).

              right, because binary data is not a string (strcat = string-concatenate). you are not going to be able to use any function or object that manipulates C-style strings - no "str*" functions, no "string" objects, no *scanf, or *printf functions.

              image processing toolkits | batch image processing | blogging

              D Offline
              D Offline
              dfn
              wrote on last edited by
              #7

              Ah good ol' for loops. Thank you very much for your help. I actually learned something :)

              1 Reply Last reply
              0
              • D dfn

                I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!

                G Offline
                G Offline
                GameProfessor
                wrote on last edited by
                #8

                you have to write a short function that change all 00 bytes in a so called "string" into the real string "0x00". To do this, read or copy your char array into a unsigned char array that is much longer. then manipulate this unsigned char array (change all 00 bytes into 4 bytes "0x00", then copy the unsigned char array back to your designated string.

                D 1 Reply Last reply
                0
                • D dfn

                  I am having a problem reading binary files. Basically, everything works fine as long as the byte I read from the binary file is not a 0x00 byte. For example, reading the following (in hex): A0, 30, C0, C0 ... works fine. But, reading: A0, 34, 03, 00 ... does not work fine. When I concatenate the strings, instead of sticking "rec" at the end of "data", it concatenates it BEFORE the end of "data" - I assume this is so because one of the bytes I read (00) is treated like '\0' (null). How do I fix this? I have tried using unsigned chars, but that didn't work, I can't use read with them. The string/char array MUST contain everything I read from the binary file (even 0x00), so it can be like "somebyteshere0x00morebyteshere" which is why i need "rec" added to the END of "data", even if "data" contains nulls before its end. I am using Visual Studio 2005. A snippet of my code is posted below string data = ""; char lr[5]; char rec[1028-4]; ... pageFile.read(lr, 4); lr[4] = '\0'; // terminate string ... pageFile.read(rec, length-4); // "length" is extracted from "lr"; it is correct, checked many times rec[length-4] = '\0'; // terminate string ... data = lr; data = data + rec; // <----------- problem happens here Thanks for any help!

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #9

                  Hi, I have to read a binary file in my projecto too. And then use the values of the binary to complete the names of my elements. I put you a bit of my code, I hope it helps

                  UINT nObj = 0, nSub = 0;
                  BYTE byteData = 0x00;
                  BYTE* pDataBuf;

                  //For every object
                  for (nObj = 0; nObj < Header[6]; nObj++)
                  { BYTE aReadSet [8] = {0};
                  pDataBuf = &aReadSet[0];
                  //For every subObject
                  for (nSub = 0; nSub < pDoc->m_cmlObjSet[nObj].m_cmlSubSet.GetCount (); nSub ++)
                  { //Read the Data
                  file->Read (pDataBuf, 8 * sizeof (BYTE));
                  aReadSet[0] = *pDataBuf;
                  CString szNameUnit = "";
                  szNameUnit.Format (_T("%X"), aReadSet[0]);
                  pDoc->m_cmlObjSet[nObj].m_cmlSubSet[nSub].m_szSubName = "SubObject"+szNameUnit;
                  //Some Other operations with the other bytes in ReadArray
                  }
                  //More Code...
                  }

                  With that I read every block of data, use the first member of the array (binary ID) to differenciate/identify every element is being saved... The IDs may have values from 00 to FA, and ALL are readen and written correctly. Ahm... I'm using VC++ 6

                  -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

                  D 1 Reply Last reply
                  0
                  • N Nelek

                    Hi, I have to read a binary file in my projecto too. And then use the values of the binary to complete the names of my elements. I put you a bit of my code, I hope it helps

                    UINT nObj = 0, nSub = 0;
                    BYTE byteData = 0x00;
                    BYTE* pDataBuf;

                    //For every object
                    for (nObj = 0; nObj < Header[6]; nObj++)
                    { BYTE aReadSet [8] = {0};
                    pDataBuf = &aReadSet[0];
                    //For every subObject
                    for (nSub = 0; nSub < pDoc->m_cmlObjSet[nObj].m_cmlSubSet.GetCount (); nSub ++)
                    { //Read the Data
                    file->Read (pDataBuf, 8 * sizeof (BYTE));
                    aReadSet[0] = *pDataBuf;
                    CString szNameUnit = "";
                    szNameUnit.Format (_T("%X"), aReadSet[0]);
                    pDoc->m_cmlObjSet[nObj].m_cmlSubSet[nSub].m_szSubName = "SubObject"+szNameUnit;
                    //Some Other operations with the other bytes in ReadArray
                    }
                    //More Code...
                    }

                    With that I read every block of data, use the first member of the array (binary ID) to differenciate/identify every element is being saved... The IDs may have values from 00 to FA, and ALL are readen and written correctly. Ahm... I'm using VC++ 6

                    -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

                    D Offline
                    D Offline
                    dfn
                    wrote on last edited by
                    #10

                    Already solved the problem, but appreciate the help! Thanks!

                    1 Reply Last reply
                    0
                    • G GameProfessor

                      you have to write a short function that change all 00 bytes in a so called "string" into the real string "0x00". To do this, read or copy your char array into a unsigned char array that is much longer. then manipulate this unsigned char array (change all 00 bytes into 4 bytes "0x00", then copy the unsigned char array back to your designated string.

                      D Offline
                      D Offline
                      dfn
                      wrote on last edited by
                      #11

                      Before I solved it, I was thinking about something like this. Luckily, I solved the problem. Thanks for the reply!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups