Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

binary string truncated ?

Posted on 2008-10-03
5
Medium Priority
?
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

596 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