Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to know if a file is a text file or a binary file?
QtWS25 Last Chance

How to know if a file is a text file or a binary file?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 7 Posters 10.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    soroush
    wrote on last edited by
    #1

    I want to search a string in all text files in a directory. Encoding could be either ASCII or UTF8. there is no convention in naming. for example a c++ source file could be named like 'a.jpg'. before trying the 'file' command of unix-like systems I want to know if there is a Qt way for this? or maybe cross-platform?...

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      AFAIK It not possible on Qt
      but you can try use libmagic for check MIME type of file.
      See my blog post "here":http://va-sorokin.blogspot.com/2011/03/how-to-get-mime-type-on-nix-system.html

      "Here":http://gnuwin32.sourceforge.net/packages/file.htm windows version of this libary

      --
      Vasiliy

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rahul Das
        wrote on last edited by
        #3

        But we can determine if a file is executable or not using "QFileInfo::isExecutable ()":http://doc.qt.nokia.com/latest/qfileinfo.html#isExecutable


        Declaration of (Platform) independence.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          soroush
          wrote on last edited by
          #4

          [quote author="Rahul Das" date="1317143517"]But we can determine if a file is executable or not using "QFileInfo::isExecutable ()":http://doc.qt.nokia.com/latest/qfileinfo.html#isExecutable[/quote]

          Some file formats are binary and they are not executable.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            THere is no generic way to check, if a file is binary or text.
            If you create a binary file, you can set some specific first bytes to check a file type, but I know no generic way for that.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • L Offline
              L Offline
              ludde
              wrote on last edited by
              #6

              What you could do is start looking for the text, and stop when you either find that text, or find something that is not text, and do that for all files. Exactly what you want to consider as not text depends on possible text encodings of course, but if you are limited to ASCII and UTF8, it might not be too difficult.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vsorokin
                wrote on last edited by
                #7

                This topic similar with your first question: http://developer.qt.nokia.com/forums/viewthread/10140/

                [Merged: from similar topic]

                --
                Vasiliy

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  soroush
                  wrote on last edited by
                  #8

                  [quote author="Vass" date="1318355648"]This topic similar with your first question: http://developer.qt.nokia.com/forums/viewthread/10140/

                  Please, don't mixed different question in one topic.
                  You can rename/edit this topic for your second question and discuss you first question in topic, link for which I printed above.[/quote]

                  That was my question! Not feedmeqt's :-/

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    There is no way to reliably test file types in a general way.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vsorokin
                      wrote on last edited by
                      #10

                      @soroush Yes, you are right it was your question, but about same thing.

                      --
                      Vasiliy

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        feedmeqt
                        wrote on last edited by
                        #11

                        sorry soroush !!! since my keywords used for searching this particular topic didn't find me any solution and I didn't done much research on it. Since QT guys always repsonded me quickly!!! :)

                        yes, I thought of making it as separate question. But, got more interested in finding solution(New to C++ and QT) .!!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved