Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

About text and Binary files.

Status
Not open for further replies.

WilliamGS

Programmer
Jan 21, 2004
54
PE
Hi all. I have a routine that reads a text file using CStdioFile, if the user selects a binary file, it reads strange symbols. How can I determine if the file is binary or text before open it?

Thanks in advance.
William GS.
 
The short answer is you can't.

A binary file may look exactly like a text file up until the last byte of the file is read. Only at that point can you say for sure whether the file is text or binary.

The only sure way is to always open the file in binary mode, and deal with it at that level. With a small amount of effort, you can convert the data into what would have been the result for a real text file opened in text mode.

--
 
You could check for values of input bytes less than 32 and not equal to '\t', '\n', '\r' or 26 - these control characters are not supposed to be inside of text files. Also, if your text files should consist only of latin text, you can check for character codes over 126.
 
Tabs, Carriage returns and New lines aren't supposed to be in a text file?? That doesn't sound right.
 
A text file should contain only characters for which isprint() or isspace() is true.

It also (IIRC) needs to be arranged as variable length records terminated by the appropriate \r\n combination for your operating system.

--
 
> Tabs, Carriage returns and New lines aren't supposed to be in a text file?? That doesn't sound right.

I wrote control characters, that are NOT Tabs, Carriage returns, New lines and Ctrl-Z (end of file character), i.e., the rest of codes below ' '.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top