Solved

binary string truncated ?

Posted on 2008-10-03
5
298 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now