Solved

binary string truncated ?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 115
C++ mouse_event mouse look 7 131
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 87
import as existing maven project 3 77
  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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
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.

734 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