• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 711
  • Last Modified:

how to tell if a file is unicode or ascii

Hi,

I'm using Visual C++ 6.0 with Windows 2000.

I need to write a program that reads from a text file. The file will be encoded entirely in either ascii or unicode. I need to be able to read and interpret both.

How can determine which format is used (without using MFC)?

Thanks in advance,
m.
0
mikjimbob
Asked:
mikjimbob
1 Solution
 
jkrCommented:
Load the file into memory and use the Win32 API 'IsTextUnicode()' (from the docs):

DWORD IsTextUnicode( CONST LPVOID lpBuffer,
// pointer to an input buffer to be examined

int cb,
// the size in bytes of the input buffer

LPINT lpi
// pointer to flags that condition text examination and receive results

);

The IsTextUnicode function determines whether a buffer probably contains a form of Unicode text. The
function uses various statistical and deterministic methods to make its determination, under the control
of flags passed via lpi. When the function returns, the results of such tests are reported via lpi.
If all specified tests are passed, the function returns TRUE; otherwise, it returns FALSE.

If you don't want to load the whole file, use a reasonable amount of bytes, which must be dividable
by 2.

Feel free to ask if you need more information!
0
 
mikjimbobAuthor Commented:
Perfect.

Cheers,
m.

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now