Strange behaviour for a Unicode project

Hi everybody,
I’ve got this Unicode project that deals with some ASCII and Unicode text files. It used to work fine but all of a sudden it started to have a strange behaviour: It’s doing what it is supposed to do when I run it in debug mode but it’s not working the same way if I try to run the generated “.exe” even though, of course, I’m not changing anything. I’ve tried to rebuild the whole project several times but I still get the same result. The piece of code that is working different involves a Unicode text file and it looks like:

// ...

// try to open the file
CFile *pFile = NULL;
pFile=new CFile(strFilePathName,
                CFile::modeReadWrite);

// get the file's size
UINT nFileLength=0;
nfileLength=(UINT)pFile->GetLength();

// allocate space for the buffer
// pBuffer is declared like
// “_TCHAR *pBuffer”
pBuffer=new _TCHAR[nFileLength /
                   sizeof(_TCHAR)];

// read the file
pFile->Read(pBuffer, nFileLength);

// construct a CString object that
// mirrors the file
CString strFile = pBuffer;

// ...

Now, for some reasons, when I run in debug mode, the string strFile gets set with the actual content of the file whereas when I run the released “.exe”, the same string is empty. Does anybody has any clue on this one?

TIA,
Eddie.
LVL 1
MensanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
One thing:

pBuffer=new _TCHAR[nFileLength /
                   sizeof(_TCHAR)];

should read

pBuffer=new _TCHAR[nFileLength * // !!!
                   sizeof(_TCHAR)];
0
chensuCommented:
pBuffer=new _TCHAR[nFileLength / sizeof(_TCHAR) + 1];

// read the file
pFile->Read(pBuffer, nFileLength);

// Don't forget the null-terminator
pBuffer[nFileLength] = _T('\0');

// construct a CString object that
// mirrors the file
CString strFile = pBuffer;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MensanaAuthor Commented:
For jkr:

I don’t really think so!

The CFile::GetLength() returns the file’s length in bytes and not in number of _TCHARs. To get the number of _TCHARs (used to allocate the buffer) you have to divide the length of the file by the size of a _TCHAR.

Sorry, but you’re wrong.
Eddie.

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

MensanaAuthor Commented:
Well, thank you chensu!

You were right. By adding the null-terminator character everything started to work again. Now, if you have a little spare time, maybe you’ll explain how come my project worked fine in debug and didn’t work in release. What’s the difference?

Thanks again,
Eddie.

0
MensanaAuthor Commented:
For chensu:

I have an observation to make. The null-terminator should be added at a different offset.
Because the CFile::Read function reads bytes and not TCHARs the dimension of my buffer will be nFileLength / sizeof(_TCHAR) instead of nFileLength. Having this said the code should look like:

pBuffer=new _TCHAR[nFileLength / sizeof(_TCHAR) + 1];

// read the file
pFile->Read(pBuffer, nFileLength);

// Don't forget the null-terminator
pBuffer[nFileLength / sizeof(_TCHAR)] = _T('\0');

// construct a CString object that
// mirrors the file
CString strFile = pBuffer;


Thanks,
Eddie.
0
chensuCommented:
>how come my project worked fine in debug and didn’t work in release. What’s the difference?

The debug mode normally allocates a little more memory than requested. And it happened the end of the buffer was zero. This kind of bugs very much depends on the current status of the system. It seems to work fine sometimes.

>pBuffer[nFileLength / sizeof(_TCHAR)] = _T('\0');

That's absolutely right.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.