Solved

UNICODE file and pointer arithmetic headache !

Posted on 2006-06-28
6
260 Views
Last Modified: 2010-04-01
Ah hello.

I have a query regarding UNICODE files.

Please consider the following code, intended merely to read a file into a BYTE array.  I use some MFC classes here, but that is not really relevant.

      CString strFile( _T( "C:\\Input.txt" ) )
      CFile file;
      if ( ! file.Open( strFile, CFile::modeRead ) )
            return -1;

      UINT nFileLengthBytes = file.GetLength();

      BYTE* b = new BYTE[ nFileLengthBytes ];

      if ( ! b ) return -1;

      TCHAR bom;
      BOOL bUnicode = FALSE;

      file.Read( &bom, sizeof(_TCHAR) );
      bUnicode =  bom == _TCHAR( 0xFEFF );
      file.SeekToBegin();

      UINT uBytesRead = file.Read( b, nFileLengthBytes );

      BYTE* pIn = b;

      cout << pIn << endl;
      for( UINT uPos = 0; uPos < uBytesRead;  uPos++)
      {
            cout << (pIn)++ << endl;
      }


As you can see, I read the whole file byte for byte into a BYTE array.  Simple.

Currently, the contents of the input file is just one line, without a carriage return, saved as ANSI in notpad:

This is a sentence.


The output I get here on the first run of the for loop is

This is a sentence.²²²²½½½½½½½½&#9632;
This is a sentence.²²²²½½½½½½½½&#9632;

On the second run of the for loop, I get

his is a sentence.²²²²½½½½½½½½&#9632;

The third run:

is is a sentence.²²²²½½½½½½½½&#9632;

and so forth.  I can see why this is happening; I am incrementing the pointer which currently points at the start of the sentence, so on each increment, it points one character further along the sentence.

Now come the questions:

1) Why is there a load of rubbish on the end of pIn ?  It points to b, which is a buffer allocated to the exact size of the text I read in.  Where is this rubbish coming from ?

2) I save the file C:\Input.txt as UNICODE via Notepad.  Now, the output I get is completely different

First run:

 &#9632;T
 &#9632;T

Second run:

&#9632;T

Third run:

T

Fourth run:

<single empty char>


Fifth run:

h

etc.  It appears that I output each character in the sentence followed by an empty character.  (Obviously, I only notice this since I output each byte on a new line.  If they were all on the same line, this would be transparent.)

Now, I think this is because of the UNICODE character encoding, each character in the sentence is two bytes, but because they are English characters, the second byte is not used, so it just empty.  Please correct me if I am wrong.  *** Does the ++ notation used on the pointer increment by one BYTE because I have a BYTE array ?  If I had a char array, would ++ increment by one char ?

But what I don't get is why the exact same method as before outputs one byte at a time, whereas before I got the whole sentence, just trimmed from the left, if you get me.

Any ideas ?

TIA
0
Comment
Question by:mrwad99
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Assisted Solution

by:e_tadeu
e_tadeu earned 250 total points
ID: 17000522
For 1):

The rubbish is there because your string is not finalized with a null char. As you said, the buffer is exactly the size of the string, so there is no space for the ending null char. What you should do is:

   BYTE* b = new BYTE[ nFileLengthBytes + 1];

// ....

   UINT uBytesRead = file.Read( b, nFileLengthBytes );
   b[uBytesRead] = '\0'

for 2):

Because it is unicode, you can't use BYTE. You should really be using wchar for unicode text!

0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 17000538
1) Why is there a load of rubbish on the end of pIn ?
You need null character in the end, cout operator handles BYTE* are string. You can do the following:

BYTE* b = new BYTE[ nFileLengthBytes + 2];
b[nFileLengthBytes + 1] = b[nFileLengthBytes] = 0;

Two bytes will be OK both for ANSI and Unicode.

2) because they are English characters, the second byte is not used, so it just empty.
Yes, it is equal to 0.

>>If I had a char array, would ++ increment by one char ?

unsinged short* or WCHAR* would increment for two bytes. Pointer is incremented to number of bytes equal to sizeof(type).
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17000625
Thanks both.  

>> But what I don't get is why the exact same method as before outputs one byte at a time, whereas before I got the whole sentence, just trimmed from the left, if you get me.

Any comments on that ?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 48

Expert Comment

by:AlexFM
ID: 17000682
Because after every byte there is null byte. cout handles BYTE* pointer as string and null is end of string.
0
 
LVL 4

Expert Comment

by:e_tadeu
ID: 17000691
I think it outputs one byte at a time, becase on unicode your string is something like this:
'T\0h\0i\0s\0 \0i\0s\0 \0'...

You see, with \0's in between.. so, it will only output one char and end the string because of the \0, if you don't use proper unicode IO (e.g, wcout, etc)

0
 
LVL 19

Author Comment

by:mrwad99
ID: 17000852
Thanks both :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now