Solved

binary string truncated ?

Posted on 2008-10-03
5
299 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help returning a SOAP Fault using JAX_WS, NetBeans, WSDL 6 59
PDF library for Delphi 2 105
Find Visual Studio Tools 2 92
Exception thrown at 0x00007FFD5BC81F28 7 38
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

910 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