Results 1 to 5 of 5

Thread: QIODevice

  1. #1
    Join Date
    Jan 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QIODevice

    Hi,
    I am writing more than one line to text file with QIODevice::ReadWrite,
    Although there are a lot of lines, in text file they are at the same line,
    So reading it is so complex.
    \n does not work,

    Can anyone help plese?
    For example:
    My data is like :
    a21
    b34
    apple
    45r

    But when I write it to text :
    a21 b34 apple 45r

    I want to prenevt this.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QIODevice

    Are you adding QIODevice::Text to the OpenMode? If you want newlines in the output, you have to explicitly write them if you are using QIODevice directly to write the files:

    Qt Code:
    1. iodevice.write( mystring );
    2. iodevice.write( "\n" );
    To copy to clipboard, switch view to plain text mode 

    But for text files, it is more usual to create a QTextStream and a QFile and use that combination to write and read text data:

    Qt Code:
    1. // Writing:
    2.  
    3. QFile file("out.txt");
    4. if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    5. return;
    6.  
    7. QTextStream out(&file);
    8. out << mystring << "\n";
    9.  
    10. // Reading:
    11. QFile file("in.txt");
    12. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    13. return;
    14.  
    15. QTextStream in(&file);
    16. while (!in.atEnd()) {
    17. QString myString = in.readLine(); // readline automatically removes the EOL
    18. // do something with "mystring"
    19. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QIODevice

    You can try with "/r/n" for Carriage Return.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QIODevice

    You can try with "/r/n" for Carriage Return.
    Yes, you can try. What you will get is a file that contains "/r/n" on each line. You might have better luck if you try "\r\n".

    By the way, using "\n" will have exactly the same effect. On Windows, the device drivers add the "\r" automatically to give Windows-style CRLF line endings. In linux, "\n" (new line) is good enough, so that is all the drivers write.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice

    Quote Originally Posted by d_stranz View Post
    Yes, you can try. What you will get is a file that contains "/r/n" on each line. You might have better luck if you try "\r\n".

    By the way, using "\n" will have exactly the same effect. On Windows, the device drivers add the "\r" automatically to give Windows-style CRLF line endings. In linux, "\n" (new line) is good enough, so that is all the drivers write.
    Of course in TEXT mode only.

Similar Threads

  1. GUnzipDevice - QIODevice
    By zaphod77 in forum Qt-based Software
    Replies: 0
    Last Post: 1st April 2011, 21:22
  2. QIODevice and QTextCodec?
    By whitefurrows in forum Qt Programming
    Replies: 5
    Last Post: 26th November 2010, 18:50
  3. QIODevice
    By sabeesh in forum Newbie
    Replies: 1
    Last Post: 26th September 2007, 10:01
  4. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29
  5. QIODEvice
    By mickey in forum Qt Programming
    Replies: 2
    Last Post: 5th July 2006, 19:00

Tags for this Thread

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.