Results 1 to 5 of 5

Thread: Text file

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Text file

    How to know a file is a text file ? Like *.txt, *.c, *.cpp, etc. Not binary file like *.pdf, *.jpg, *.exe, etc.
    In linux, file extension is not important right ? So, I think detecting by file extension is not a good way...

    Thank's before...

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Text file

    Usually, text files don't contain symbols with code less then 0x20, except of \r \n and \t.
    you can analyze each symbol in file, and, if there are several characters less then 0x20 ( except of \r \n and \t ) you can say, that this file is NOT a text

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Text file

    If by 'text' you mean US-ASCII (like most .c, .cpp, etc files), then just confirm the first 1KB of so of the file is between 32 and 127 (with the exceptions borisbn states, such as \r, \n & \t)
    '.txt' files are more complicated - they could be in unicode, so then it becomes a lot more difficult (Unless you just support UTF8, then you can recognise the byte ordering and individual characters easily)

  4. #4
    Join Date
    Jul 2010
    Posts
    12
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Text file

    In Linux, there's an utility called 'file', that uses a set of tests to figure out file type. For instance, on my box:

    $ file .bashrc /usr/bin/xfwm4 Videos/it/yasnippet.avi

    gives:

    .bashrc: ASCII text
    /usr/bin/xfwm4: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped
    Videos/it/yasnippet.avi: RIFF (little-endian) data, AVI, 588 x 560, ~15 fps, video:

    In text files, there always presents word "text", in executables "executable" and in various binary data types "data". You can get rid of filenames with --brief or have a nice bedtime reading with man file. So, you can just pipe the output into your app and search for strings like "text". If I remember right, there is a libfile that actually does the job, and you probably can just link to it, but I'm not sure. Also I don't know about any crossplatform way to do it

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Text file

    On Linux, there's also a mime library so you can figure out the type of a file and it'll return eg. "text/html", but again, that's really not portable beyond Linux unless you port that library yourself along with it's dependancies.

Similar Threads

  1. Parse Text File with Qt
    By rhf417 in forum Qt Programming
    Replies: 12
    Last Post: 26th June 2013, 03:47
  2. Replies: 3
    Last Post: 3rd May 2009, 08:58
  3. Appending text to a file
    By janus in forum General Programming
    Replies: 8
    Last Post: 25th March 2009, 11:23
  4. Replies: 1
    Last Post: 3rd September 2008, 14:16
  5. Huge Text File
    By mcosta in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2008, 19:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.