CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    53

    Determine text file or Binary file

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Determine text file or Binary file

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Determine text file or Binary file

    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.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Determine text file or Binary 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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Determine text file or Binary file

    Take a magnifier, remove the HDD cover, and see if the file inside is readable.

    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.

    Quote Originally Posted by mjvalan
    (file name doesn't have file extension).
    Never trust in file extension. It can be changed anytime as well.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Determine text file or Binary file

    Quote Originally Posted by mjvalan
    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
    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.

    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

  7. #7
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    53

    Re: Determine text file or Binary file

    Thank you for your replies.

    Regards
    MJVALAN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured