Results 1 to 6 of 6

Thread: New line when writing a File

  1. #1
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default New line when writing a File

    Hi guys:

    I got a problem that seems to be very easy to solve. I want to write a file (previously selected by the user) with some values in columns, so it is necessary to insert a "new line" character "\n"

    I've tried but it doesn't work. In contrast, "\t" for a tab works properly.

    I post the code here:

    QFileDialog dialog(this);
    QString output = dialog.getSaveFileName( this,"Save file export", "/tmp/test.txt", "Text (*.txt)");

    QFile outputFile(output);
    outputFile.open(QFile::ReadWrite);

    QTextStream out(&outputFile);

    for(int i = 0; i<myVector.size(); i++) //My vector is a QVector<double>
    out << myVector.at(i) << "\n" <<endl;

    The value is properly saved, but not the new line, so all values are together at the same line.

    (I've tried to delete the endl, create a previos QString and then add it, etc,....)

    Thanks!!!!!

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: New line when writing a File

    Try this:
    out << myVector.at(i) << '\n'; // note: single quotes

    Or this:
    out << myVector.at(i) <<endl; // should also work

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

    locke (17th March 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: New line when writing a File

    I've also tried it, but it doesn't work. It is a very strange error, but seems to have a quite simple solution that I can not find

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: New line when writing a File

    if you write data in text format you should open a file in text mode
    Qt Code:
    1. outputFile.open(QIODevice::ReadWrite | QIODevice::Text);
    To copy to clipboard, switch view to plain text mode 
    endl should work.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following 2 users say thank you to spirit for this useful post:

    locke (17th March 2010), YDYD (2nd July 2014)

  7. #5
    Join Date
    Feb 2010
    Posts
    64
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: New line when writing a File

    Thank you so much spirit. It works now!!!!!

    Thanks!!!

  8. #6
    Join Date
    May 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: New line when writing a File

    Thank you spirit. It helped me also.

Similar Threads

  1. writing in to a string via FILE*
    By caduel in forum General Programming
    Replies: 3
    Last Post: 12th July 2008, 09:08
  2. problem writing an xml file
    By mickey in forum General Programming
    Replies: 1
    Last Post: 28th January 2008, 16:05
  3. Writing a XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 5th February 2007, 16:23
  4. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:04
  5. Writing an XML file
    By qball2k5 in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2006, 10:58

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
  •  
Qt is a trademark of The Qt Company.