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?
 
chensuConnect With a Mentor Commented:
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
 
jkrCommented:
One thing:

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

should read

pBuffer=new _TCHAR[nFileLength * // !!!
                   sizeof(_TCHAR)];
0
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
All Courses

From novice to tech pro — start learning today.