Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing null characters to a file

Status
Not open for further replies.

lupidus

MIS
Jun 1, 2003
30
US
Regarding the null terminating character being embedded within a source/custom resource (input string) , I'm having some difficulty getting fwrite to continue past embedded 0x00 values in my data...

Someone posted on another site:
It acted like it continued beyond the end of the array until it came across the zero byte in memory. I put the "\0" or the 0x00 and it stopped exactly where I told it too. This raises the question, if that array is supposedly a hex representation of an exe, how are you getting away without stumbling across 0x00 somewhere in the binary string? this is one of the reason they tell us to encode binary data when using a null terminated string to contain it.

My code:
Code:
char * cStr = reinterpret_cast< char *>(lpVoid);
cStr = strncat(cStr, "\0", 1);
printf("size of cStr%d\n", sizeof(cStr));
FILE * stream;
if( (stream = fopen( "fread.out", "w" )) != NULL )
{
 int num_write = 0;
 num_write =  fwrite( cStr, sizeof( cStr ), 1, stream );
 printf( "Wrote items: %d\n", num_write  ); 
 fclose( stream );
}
Results:

follwing were writing sizeof(cStr) - 1 (not wanting to write the trailing null to our output file):

testing: 4d 5a 90 as input, output: 4d 5a 90 (OK)
testing: 4d 5a 90 00 as input, output: 4d 5a 90
testing: 4d 5a 90 00 03 as input, output: 4d 5a 90
testing: 4d 5a 90 03 as input: output: 4d 5a 90

writing sizeof(cStr) :
4d 5a 90 -> 4d 5a 90 00
4d 5a 90 00 -> 4d 5a 90 00
4d 5a 90 00 03 -> 4d 5a 90 00 (stripped trailing character)

Anyone have any suggestions on how to proceed?
How will the program know the difference between the trailing null at the end of the string versus embedded nulls?
 
Turns out my pointers were messed up:

cStr = strncat(cStr, "\0", 1); // doesn't do anything

replaced with:
*(cStr + size) = '\0'; // size is hardcoded for now

and changed fwrite to
fwrite( cStr, size, 1, stream );


 
The cStr is a pointer, so in
Code:
num_write =  fwrite( cStr, sizeof( cStr ), 1, stream );
you write sizeof(pointer) bytes (probably 4). It's not a size of your data! Alas, it's a common error...
Open file in binary mode ("wb" or "rb") if you want to write/read non-printable chars.
No byte zero magic...
 
> char * cStr = reinterpret_cast< char *>(lpVoid);
What is lpVoid really pointing at, before you blew away any last remaining hope of type safety?

> *(cStr + size) = '\0'; // size is hardcoded for now
Like, is the real owner of the data going to appreciate your random modification of the data?

--
 
You can't use string family functions on data with embedded nulls.

strcat is unsafe because you don't know if cSTR has room for another \0 at the end without stepping on the next memory location

You woould have o pass in the lpvoid and the size of the data pointed to and just write out the whole glob
Code:
num_wrote =  fwrite( lpvoid, data_len, 1, stream );

and as ArkM pointed out use binary mode.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top