LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to check the file contents are all ASCII?

Hi all,

I need to write a feature for my program that to check the file contents are all ASCII. Check the file extension is not a good way because people can change it easily. Are there any ways for doing that easily?

Best regards
Bill
0 Kudos
Message 1 of 4
(2,992 Views)
Well, there is no fool-proof way, but you could check for existance of non-printable characters (which might indicate that it is not a text file).

Good luck,

-Jim
0 Kudos
Message 2 of 4
(2,992 Views)
Everything in a file has an ASCII code, you probably are thinking about the subset of codes found in text files.

A good tool is the check for lexical class (found in the comparison palette). If every byte in the file is class 2 or above, it's probably not a binary file.

I have attached an image of a code example. It outputs a TRUE if the file does not contain extended or non-displayable characters.
Message 3 of 4
(2,992 Views)
Depends on what you mean by "all ASCII".

ASCII defines character codes from 0x00 to 0x7F (000-127). So any byte that's 128-255 is not ASCII, although it still could be a text file. (If I put a 248 byte in there, it will show up as a degree symbol on some systems - still text, but not ASCII.

On the other hand, if the file contains a LOT of 0x00 - 0x08, and 0x0E - 0x1F characters, it's still ASCII codes, but not likely a text file.

Your best bet, for an arbitrary file that someone hands you, is to look at the lexical class of every byte, and use the percentage of printable characters.

If you have total control over writing and reading the file, then you might use a datalog file containing a string. No matter what the extension, the file dialo
g will recognize it as a valid file. It will NOT recognize other files, even if they have the same extension. But you can't edit it in NotePad anymore...
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 4 of 4
(2,992 Views)