Solved

how to tell if a file is unicode or ascii

Posted on 2001-06-07
2
679 Views
Last Modified: 2008-03-17
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
Comment
Question by:mikjimbob
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 6165145
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
 

Author Comment

by:mikjimbob
ID: 6167110
Perfect.

Cheers,
m.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 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

11 Experts available now in Live!

Get 1:1 Help Now