I want to determine, a file is text file or binary file(file name dosen't have file extension).Is there any API in Windows SDK?
Thanks
MJVALAN
Printable View
I want to determine, a file is text file or binary file(file name dosen't have file extension).Is there any API in Windows SDK?
Thanks
MJVALAN
There is nothing like that, because everything is binary. Even the text files are binary, it's just that the binary numbers are ASCII (or UNICODE) codes.
See this FAQ for more.
It's not an absolute guarantee (and this would only be valid for files originated under DOS or Windows) but you could try parsing the file for each occurrence of the character 0x0D. If every occurence is followed by the character 0x0A, then it's probably a text file.
You might also want to search the file for non-alphanumeric characters, like 0x00 ... 0x08 or 0x0E ... 0x1F because chances are really small such characters appear in text files, but chances are very high that such characters appear in binary files.
Take a magnifier, remove the HDD cover, and see if the file inside is readable. :D
Well, this is not first time discussed problem/quiz/homework.
Because a "text file" is generally intended to show sometimes sometext somewhere, just open it with a text viewer/editor (notepad is a good and free one :)) or display it in your application in an edit control and see what's inside. Nobody can assure you that a flat (text) file always contain only printable characters plus CR/(LF)s and tabs.
IMHO: from practical point of view, to see if an unknown (type or what is intended for) file is a "text" file or not it's not relevant.
The situation can change for file types those have format specifications, for an example it's possible to be necessary to decide if a file is a TIFF, GIF, JPEG, or simply garbage.
Never trust in file extension. It can be changed anytime as well.Quote:
Originally Posted by mjvalan
As others have mentioned, there is no way to guarantee that a file is "text" or "binary", since all files are binary. That's like asking whether an animal is a dog or poodle.Quote:
Originally Posted by mjvalan
Look at how most GUI-based FTP programs work. They usually have lists of file name extensions where it is assumed that these files are text files. You have the option in these programs of changing these assumptions when neccessary.
Regards,
Paul McKenzie
Thank you for your replies.
Regards
MJVALAN