• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Checking if a file actually contains text

Status
Not open for further replies.
Level 21
Joined
Dec 9, 2007
Messages
3,096
While programming my MPQ editor, I've come to the point where I need to open files by double clicking. Piece of cake!

Now the thing is... Many language files are opened with programs like Notepad++ which is single-instanced, meaning that if the process opens, it is closed almost immediately and my MPQ editor waits until the process is gone and then continues, reimporting and deleting the file. Now the problem is, the instance of Notepad++ will try to open a non-existing file. I cannot allow this behavior.

My program also has a decent text editor which I want to use to edit text files.
I've already added some know text formats, but there might be others or users might change the extension. As a last layer of detecting text files, I read the registry values of Content Type and PerceivedType and see if they contain "text". But many programs do not care about this registry to help other programs. There are many other text files...

Now I need a way to check if a file actually contains text.
I tried using Regular Expressions but I suck at writing them.

Can anyone help me? If you can give me code, it's preferably to be in C# or VB. Worst case in C++.
 
Last edited:
Level 21
Joined
Dec 9, 2007
Messages
3,096
That's exactly what I do not need.

But seriously, WHAT IS SO HARD TO CHECK IF A FILE IS BLANK?

Code:
if (string.IsNullOrEmpty(System.IO.File.ReadAllText("C:\test empty file.txt")))
{
        MessageBox.Show("The file is empty!");
}
__________________

I need to check if the content of the file IS TEXT, and NOT EMPTY and NOT BINARY.
 
Level 22
Joined
Feb 3, 2009
Messages
3,292
A simple space in the ASCII table, which is detected with Contains(" ")

EDIT: My mistake, I just remembered Null and Space are different letters, you must use a way to detect it by ASCII characters, I'm not good in C++ so you'll have to figure it out yourself on how to detect via ASCII.

Null = Chr(0)
Space = Chr(32)
 
Status
Not open for further replies.
Top