Solved

Determine if a file is ascii or binary

Posted on 1997-11-09
7
585 Views
Last Modified: 2013-12-04
hi,

I am writing a VC++ 5 application. I need to determine if a file is a binary file or an ASCII file. Any ideas?
I tried opening the file and using the function isascii, but this
function succeeds for binary files as well.

Thanks, Ralph
0
Comment
Question by:rakhras
7 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1408292
The general way to determine if a file is a binary file or an ASCII file is to check if the file contains '\0' (a byte which value is zero). If yes, it is a binary file. If not, it is an ASCII file.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1408293
A file could be binary and might not contain an binary 0.  I would search through for any character that is not a an ASCII letter (' ' to '~') or that is not a carriage return or line feed.  If a character is found that is not and ASCII letter, CR or LF, then the file is binary.  If all the characters are ASCII, CR's, or LF's, then the file is PROBABLY ASCII.  Note this is not perfect.  A binary file might be made up of bytes that all just happen to be ASCII letters.

An aditional test might be to look to see if the last character is a CR (or possibly a CR/LF).  Many ASCII files will end with a CR (or CR/LF).  This depends on the program that produces the files, though.  
0
 
LVL 2

Expert Comment

by:wpinto
ID: 1408294
Hey rakhras,

In case you forgot, just wanted to remind you that there is locked question 'Debugging Release version', which you haven't evaluated yet :-)

Wilfred
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 23

Expert Comment

by:chensu
ID: 1408295
nietod:
Don't forget that there are many languages besides English. And, I am not sure if the ASCII files rakhras mentions are really ASCII text files because there are double-byte text files and Unicode text files.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1408296
Hey, I'm just an ignorant American, my country, and my language are the center of the universe.

But anyway I'll revise my comment to make it more politically correct, even if I'm not.  

I still think looking for a binary zero is insufficient.  Finding one pretty much garantees that the file is binary, but not finding one does not necessarily mean the file is ASCII.

There are a bunch of procedures in the C++ standard library for testing characters.  These procedures work with unicode and multi-byte characters.  (I'm not familiar with any of them, because I'm an ignorant American, see above disclaimer)  Anyways, these procedures can be used to test each byte, or multi-byte in the file determine if it is a character.  If anything that is not a characters is found, the files is binary.  If everything is a character (or CR or LF), the file is probably ASCII.
0
 

Author Comment

by:rakhras
ID: 1408297
thanks guys for your responses.
I've implemented a mix of your suggestions and it works fine.
So what is the protocol here for grading the answer; i've used suggestions from both chensu and nietod. If we should split the points, how do i do that? thanks,
ralph.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1408298
Unfortunately, you can't split points.  They are considering it.
But your thanks is enough for me.  (Although you could always send money <g>.)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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