Solved

binary string truncated ?

Posted on 2008-10-03
5
300 Views
Last Modified: 2013-12-14
I need to read a 50 byte binary string from a file.
A function is invoked which is only returning a maximum 42 bytes:

method () {
CString      GetString(const CString &src = "");
.... }
What is the problem ?
0
Comment
Question by:pillmill
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 22638572
If the string is binary, it might contain NULL bytes, which automatically truncate the string at their position. Can you verify that?
0
 

Author Comment

by:pillmill
ID: 22638847
Yes - the truncation occurs when, intermitantly, a 0x0A appears in the string.

In this case, how can the entire string, including the NULL,  be placed into the CString variable ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 22639439
As stupid as it may sound - don't read a string, but a char array of 51 bytes instead. What is the underlying code?
0
 
LVL 5

Accepted Solution

by:
isprabu earned 500 total points
ID: 22648621
pillmill,
0x0A is newline (LF or line feed) character. Usually, any file or string operations stop reading when they encounter CR or LF.
As jkr has suggested, may be you should switch to reading all the bytes as unsigned char or BYTE. And then, if required, you can also transform/translate these CR or LF characters into \r or \n escape sequences; after the translation, you can store the bytes in a CString and use it further.
Hope this helps!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22652220
When reading with MFC classes you should open and read the file like

    CFile file;
    if (!file.Open("file.dat", CFile::typeBinary | CFile::modeRead))
    {
         AfxMessageBox("file.dat cannot be opened for read");
         return;
    }
    unsigned char buffer[50] = { 0 };  // make all zeros
    int count = file.Read(buffer, sizeof(buffer));
    if (count == 0)
    {
          AfxMessageBox("file.dat cannot be read");
         return;
    }  

With that buffer should contain all 50 bytes.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question