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?
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.