Nulls after each character when writing CString to CFile using Write member

Adam Stepniak

I'm currently studying MFC library CFile class and I have a problem with writing data to a file using Write method. When I pass char array as parameter it works perfectly fine:

char c[] = "Hello!"; 
int size = sizeof(c) / sizeof(c[0]);
myFile.Write(c, size)

Characters that were written to file:

Hello!

But when I try to pass CString object as an argument:

CString cS("Hello");
myFile.Write(cS, cS.GetLength());

I get:

H e l

I also tried:

CString cS("Hello");
LPWSTR c = cS.GetBuffer();
myFile.Write(c, cS.GetLength());
cS.ReleaseBuffer();

But the output is the same as above. What may be cause that conversion? Does it happen because text is stored in wide characters?

Vada Poché

Problem:

The second parameter to CFile::Write is the number of bytes that the function will transfer from the first parameter (the buffer). You're passing cS.GetLength(), which would rather pass the number of characters in the string, and this is not the same as the number of bytes that the string itself may be composed of.

Solution:

You should change the line that writes the string into file to something like this:

myFile.Write(LPCTSTR(cS), cS.GetLength()*sizeof(TCHAR));

sizeof(TCHAR) will yield a different number based on if you're building for Unicode or MBCS. This is because TCHAR is defined as wchar_t for Unicode builds, and as char for MBCS builds. As such, multiplying the length of string with the size of TCHAR will always equal the number of bytes the string is composed of regardless of if you're building for Unicode or not.

Other points to note:

You've no reason to call GetBuffer() and ReleaseBuffer() here, whatsoever.

This point is not a major, but the CFile::Write function takes a const void * as its first argument. So you should rather be casting your CString to LPCTSTR (which would automatically evaluate to LPCWSTR or LPCSTR based on if you're building with Unicode or MBCS).

One last thing: It's good to wrap string literals with the _T() macro so that you could compile for Unicode and MBCS alike without having to change your code.

Applying all changes, your whole code would look something like this:

CString cS(_T("Hello"));
myFile.Write(LPCTSTR(cS), cS.GetLength()*sizeof(TCHAR));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

When writing section of txt-file to csv, each single character is assigned a new column for each line

Writing terminator character (i.e. ox1e) fails after first write

No audio when writing video using write_videofile

File gets overwritten when writing to file using write()

writing to previously existing json file(with array of objects and each object with an array member) using qt 5.3

Using SED to cut after 7th character of each line

Delete everything after a certain character on each word using sed

Using sed to delete lines - get 00 after each character

Memory leak when using CString for tree node

Spark write CSV not writing unicode character

CFile & CStdioFile produces different results in writing & reading

PermissionError [Errno 13] when writing to a file after using lockfile in python

is there a simpler way to write my code instead of manually writing each if else when I add a parameter

NPE when using map {} on ArrayList containing NULLs

Writing to a csvFile using two lists. How do i write each list to two different columns

replace each character with the first greater character after it

Using CString object in CString::Format

How to provide options when writing to S3 from R using s3write_using?

How can I write each character from string to separate div using JavaScript?

getting special character instead of single quote when writing to HTML file using nodeJS and cheerio

first and last character "p" is deleted when writing file on filesystem using python

Cannot write in the console after writing in the file

Validating an ISO write directly after writing

Strange character added when writing to file

Generate Sequence in a pyspark dataframe such that it increments when a value is found after nulls

Write After End Error when Reading a File using FS

React : How to make password characters visible when typing each character and hide(*) after few time interval in react?

Writing a Hexadecimal Escape Character Sequence using Variables

writing out a character value inserting indentation when there is special character in r