Solved

how to tell if a file is unicode or ascii

Posted on 2001-06-07
2
691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
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.

632 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