Results 1 to 6 of 6

Thread: Create file and read text with QFile and QTextStream

  1. #1
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Create file and read text with QFile and QTextStream

    Hi,

    in the docs is written how to read text data from a file, how to write text data to a file... but it does not say how to create a file. It always starts with qfile:pen but if we don't have that file we need to create it first. How can we do that?

    On the other hand, seems like I just need to add "\n" in the middle of the string associated to QString in order to have different rows, is that true? does it work differently in Windows OS?

    thanks!

  2. #2
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Create file and read text with QFile and QTextStream

    Quote Originally Posted by jano_alex_es View Post
    but if we don't have that file we need to create it first. How can we do that?
    Doc of QFile:pen says:
    Note: In WriteOnly or ReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.
    Quote Originally Posted by jano_alex_es View Post
    On the other hand, seems like I just need to add "\n" in the middle of the string associated to QString in order to have different rows, is that true? does it work differently in Windows OS?
    It works this way: when you only put \n at the end of a line, Notepad on Win32 will not display the text correctly. If you put \r\n at the end, vi will display ^M at the end of each line.
    If only QT-Apps use the file, you can use endl and ignore all other editors, else you'll have to replace \r\n or \n to what you want before loading/saving.

  3. The following user says thank you to auba for this useful post:

    jano_alex_es (26th May 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create file and read text with QFile and QTextStream

    Quote Originally Posted by auba View Post
    It works this way: when you only put \n at the end of a line, Notepad on Win32 will not display the text correctly. If you put \r\n at the end, vi will display ^M at the end of each line.
    If only QT-Apps use the file, you can use endl and ignore all other editors, else you'll have to replace \r\n or \n to what you want before loading/saving.
    Small corrections :

    • when opening the device (QFile or any QIODevice) in Text mode, you can just use the LF ('\n') character as it is automatically converted into local line endings (may it be LF, CR or CRLF)
    • conversely, when reading a file opened in Text mode local line endings are converted into LF to make it easier to process the content without having to mess with line endings
    • this issue is purely independent of Qt, nowadays most applications are able to read files of any line endings (even Wordpad does it)
    Current Qt projects : QCodeEdit, RotiDeCode

  5. The following user says thank you to fullmetalcoder for this useful post:

    jano_alex_es (26th May 2009)

  6. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Create file and read text with QFile and QTextStream

    Doc of QFileen says:
    My bad! I didn't see that open() overload... I just read the other two :S

    thanks!

  7. #5
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Create file and read text with QFile and QTextStream

    Quote Originally Posted by fullmetalcoder View Post
    file opened in Text mode local line endings are converted into LF to make it easier to process the content without having to mess with line endings
    Cool, that's new to me too, but very useful. Indeed, I did not care about linefeeds since I'm using qt4, so my info was a little bit old

    Quote Originally Posted by fullmetalcoder View Post
    nowadays most applications are able to read files of any line endings (even Wordpad does it)
    if you mean "nowadays" = Vista... most of our customers are still using XP, some of them even w2k

  8. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create file and read text with QFile and QTextStream

    Quote Originally Posted by auba View Post
    if you mean "nowadays" = Vista... most of our customers are still using XP, some of them even w2k
    "Nowadays" is not related to OS but to applications. Wordpad from WinME was already able to deal with LF line endings properly (I did not try CR ones but I guess it should have worked as well) and, honestly, apart from Notepad obviously, I have never EVER encountered a worse/more outdated text editor than Wordpad so it acts as a "lowest common denominator" here.
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14
  2. QSettings vs (QFile + Qtextstream)
    By nupul in forum Newbie
    Replies: 5
    Last Post: 10th April 2006, 07:26
  3. segfault on qtextstream
    By patcito in forum Qt Programming
    Replies: 13
    Last Post: 26th February 2006, 13:10

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.