locked
Reading in special characters RRS feed

  • Question

  • I have a couple functions I use for reading in data from the middle of a file in binary fashion... The problem I'm having is that some of the binary (compressed in this case) data that is read in could possibly include a "\".  My question is, if I just check for a "\" and replace it with "\\" will the std::string be normal?

    BOOL seek(HANDLE hfile,LONG bytepos)
    {
         return SetFilePointer(hfile,bytepos,NULL,FILE_BEGIN);
    }

    std::string get$(HANDLE hfile,DWORD numbytes)
    {
            DWORD bytesread;
            char *lpb = new char[numbytes];
            ReadFile(hfile,lpb,numbytes,&bytesread,NULL);
            std::string b{lpb,bytesread};
            delete lpb;
            return b;
    }

    Monday, August 29, 2016 8:35 PM

Answers

  • On 8/29/2016 4:55 PM, Brian Muth [MVP] wrote:

    However, I don't like the idea trying to wrap a std::string around the byte array. What happens if you have a null in the middle?

    std::string has no problem representing embedded NULs. None of its methods are making any assumptions about the absence thereof.

    • Marked as answer by TallGuy63 Monday, August 29, 2016 9:39 PM
    Monday, August 29, 2016 9:25 PM

All replies

  • My question is, if I just check for a "\" and replace it with "\\" will the std::string be normal?


    No! Don't replace with a double backslash.

    A double backslash is used in source code only, since a single backslash is considered the beginning of a special character, as in \t, \r rather than part of the string itself.

    However, I don't like the idea trying to wrap a std::string around the byte array. What happens if you have a null in the middle?

    Monday, August 29, 2016 8:55 PM
  • Hmmm... interesting thought...

    What would be the best way to get data from a file to a std::string then??

    Monday, August 29, 2016 9:15 PM
  • On 8/29/2016 4:35 PM, TallGuy63 wrote:

    I have a couple functions I use for reading in data from the middle of a file in binary fashion... The problem I'm having is that some of the binary (compressed in this case) data that is read in could possibly include a "\"

    Why would that be a problem? What difficulty do you believe this would cause, exactly?

    Monday, August 29, 2016 9:24 PM
  • On 8/29/2016 4:55 PM, Brian Muth [MVP] wrote:

    However, I don't like the idea trying to wrap a std::string around the byte array. What happens if you have a null in the middle?

    std::string has no problem representing embedded NULs. None of its methods are making any assumptions about the absence thereof.

    • Marked as answer by TallGuy63 Monday, August 29, 2016 9:39 PM
    Monday, August 29, 2016 9:25 PM