Results 1 to 12 of 12

Thread: Write one value per line in text file

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Question Write one value per line in text file

    Code :
    Qt Code:
    1. QString filename = "textfile.txt";
    2. QFile file(filename);
    3. QString val;
    4. float v;
    5.  
    6. if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    7. {
    8. for ( int i =0; i<Results.localresults.size() ; i++)
    9. {
    10. v = Results.localresults.at(i);
    11. val = QString::number(v);
    12. char *valtoAsciidata = val.toAscii().data();
    13. QTextStream out(&file);
    14. out << v << endl ;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    I have written code as above to actually write values to a text file. But it writes the values continuously . What I want is one value to be written in one single line, then the next value on the next line.
    How to code in such a way that it writes one value per line ?
    Last edited by babygal; 29th November 2010 at 08:19.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Write one value per line in text file

    QTextStream is a Qt object; std::endl is a standard library object. I doubt that QTextStream understands std::endl as an end of line character. There's a Qt function endl() that takes a QTextStream as an argument - and probably does what you want - but that's not what you're using.

    It's also somewhat surprising that your code works at all, given that you reinitialize 'out' on every loop iteration.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Write one value per line in text file

    There is QTextStream::endl as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write one value per line in text file

    Quote Originally Posted by SixDegrees View Post
    It's also somewhat surprising that your code works at all, given that you reinitialize 'out' on every loop iteration.
    You understand that 'out' is actually constructed and destroyed per iteration, right? This is well defined, albeit probably not desirable, behavior. The compiler probably moves the ctor and dtor outside the loop body anyway.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Write one value per line in text file

    Quote Originally Posted by Timoteo View Post
    The compiler probably moves the ctor and dtor outside the loop body anyway.
    The compiler cannot change the number of times the constructor/destructor is called. It can only optimize in a situation when there is no constructor defined (i.e. for POD types).

    Qt Code:
    1. QFile f(...);
    2. f.open(...);
    3. QTextStream stream(&f);
    4. for(int i=0;i<size;++i){
    5. stream << restults.at(i) << endl;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Write one value per line in text file

    Quote Originally Posted by Timoteo View Post
    You understand that 'out' is actually constructed and destroyed per iteration, right? This is well defined, albeit probably not desirable, behavior. The compiler probably moves the ctor and dtor outside the loop body anyway.
    Very unlikely. There isn't enough information to guide the compiler into hoisting the constructor out of the loop, so the stream is almost certainly being created and destroyed on every iteration. How the stream behaves under such circumstances, I have no idea, but at a minimum there's a large performance hit here, and there is also the possibilty that the file will be written in some unpleasant way due to repeated initializations. I have no idea whether the latter actually occurs here, but it's always best to simplify as much as possible before diving into a fix or modification.

  7. #7
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Write one value per line in text file

    Quote Originally Posted by SixDegrees View Post
    Very unlikely. There isn't enough information to guide the compiler into hoisting the constructor out of the loop, so the stream is almost certainly being created and destroyed on every iteration. How the stream behaves under such circumstances, I have no idea, but at a minimum there's a large performance hit here, and there is also the possibilty that the file will be written in some unpleasant way due to repeated initializations. I have no idea whether the latter actually occurs here, but it's always best to simplify as much as possible before diving into a fix or modification.
    Ok, but the point is that you were saying that it shouldn't work as written. If this were the case, then smart pointers would not work either. (Having just typed that I understand why compilers cannot relocate ctor and dtor as wysota mentioned).

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Write one value per line in text file

    Quote Originally Posted by Timoteo View Post
    Having just typed that I understand why compilers cannot relocate ctor and dtor as wysota mentioned
    This is the simplest example that comes to my mind why it can't be done:
    Qt Code:
    1. class X {
    2. public:
    3. X() { X::cnt++; }
    4. private:
    5. static int cnt;
    6. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Write one value per line in text file

    I modified the code(as below) but the problem still not solved.The values are still written continuously (and not one value per line as what I want).
    Qt Code:
    1. QString filename = "textfile.txt";
    2. QFile file(filename);
    3. QString val;
    4. float v;
    5. QTextStream stream(&file);
    6. if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    7. { for ( int i =0; i<Results.localresults.size() ; i++)
    8. { v = Results.localresults.at(i);
    9. val = QString::number(v);
    10. char *valtoAsciidata = val.toAscii().data();
    11. stream << v << endl;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Write one value per line in text file

    What is the point of lines 9 and 10 in the above code?

    If you are on windows it might be smart to pass the QIODevice::Text flag while opening the file. Without it you probably get \x0A instead of \x0D\x0A for end of line.

    This works fine for me:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(){
    4. QFile f("res.txt");
    5. f.open(QFile::WriteOnly|QFile::Truncate|QFile::Text);
    6. QTextStream str(&f);
    7. list << "abc" << "def" << "ghi";
    8. foreach(const QString &s, list){
    9. str << s << endl;
    10. }
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 30th November 2010 at 09:59.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    babygal (1st December 2010)

  12. #11
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Write one value per line in text file

    hi,
    this code works fine in debian.
    may i know , what is the type of Results?

    Bala

  13. #12
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Write one value per line in text file

    Quote Originally Posted by wysota View Post
    What is the point of lines 9 and 10 in the above code?
    No point for lines 9 and 10 actually. Can be removed.


    Added after 4 minutes:


    Quote Originally Posted by BalaQT View Post
    ...
    may i know , what is the type of Results?
    Results is a public member of class clResults in this scope :

    Qt Code:
    1. public :
    2. clResults Results;
    To copy to clipboard, switch view to plain text mode 


    Added after 12 minutes:


    Quote Originally Posted by wysota View Post
    Qt Code:
    1. #include <QtCore>
    2.  
    3. int main(){
    4. QFile f("res.txt");
    5. f.open(QFile::WriteOnly|QFile::Truncate|QFile::Text);
    6. QTextStream str(&f);
    7. list << "abc" << "def" << "ghi";
    8. foreach(const QString &s, list){
    9. str << s << endl;
    10. }
    11. return 0;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Your code as above works fine. It can do what I want. Thank you so much for the useful post.
    Last edited by babygal; 1st December 2010 at 06:47.

Similar Threads

  1. How to write ByteArray into a text file?
    By babygal in forum Newbie
    Replies: 4
    Last Post: 11th October 2010, 04:19
  2. Remove a line in a text file
    By jaca in forum Newbie
    Replies: 1
    Last Post: 18th March 2010, 22:13
  3. How to read line number in a text file
    By grsandeep85 in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2009, 09:09
  4. How to write something in line of txt file
    By Zergi in forum Qt Programming
    Replies: 2
    Last Post: 24th December 2007, 10:02
  5. Replies: 2
    Last Post: 17th November 2006, 11:25

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.