Results 1 to 3 of 3

Thread: out Text file

  1. #1
    Join Date
    Mar 2010
    Location
    spain
    Posts
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default out Text file

    According to this code .....

    Qt Code:
    1. QDateTime dateTime = QDateTime::currentDateTime();
    2. QString Hora = dateTime.toString("hh:mm:ss");
    3. QString Fecha = dateTime.toString("dd-MM-yy");
    4.  
    5. QString Path=QCoreApplication::applicationDirPath().toLatin1();
    6.  
    7. QFile file(Path + "/Logs/" + Fecha + ".txt");
    8. file.open(QIODevice::WriteOnly| QIODevice::ReadOnly| QIODevice::Text);
    9.  
    10.  
    11.  
    12. QTextStream out(&file);
    13.  
    14. while (!out.atEnd()) {
    15. QString line = out.readLine();
    16.  
    17. }
    18.  
    19.  
    20. out << Hora ;
    21. out << "\n";
    22.  
    23.  
    24. file.close();
    25.  
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    The output file for example is:

    10:30:20
    10:30:21
    10:30:22
    10:30:23
    ....
    ...
    ....
    How can we make the output be:?
    10:30:23
    10:30:22
    10:30:21
    10:30:20
    ....
    ...
    ....

    thanks

  2. #2
    Join Date
    Feb 2011
    Posts
    64
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 2 Times in 2 Posts

    Default Re: out Text file

    First, read the file in QStringList then use the method sort().

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: out Text file

    Line 5 and 7 select a location for log files that is not likely to be writeable when the application is deployed to "Program Files" on Windows Vista/7.
    Line 8 could use QIODevice::ReadWrite for better readability. Random access to text files is unusual though.
    Are you trying to append to the file by moving to the end of the existing file with the while() loop at line 14? Have you tried QIODevice::Append as the open mode?

    The code you show writes a single line to the file, and not strongly connected to your question. Your question is either about:
    • Sorting the lines in the log file after a number are written: see valgaba's response.
    • Writing each new line to the top of the text file rather than the bottom.


    If you want to insert a text record to the front of a file you need to:
    • Create a temporary file
    • Write the new record to the temporary file.
    • Open the target file
    • Read all the lines from the target file and append these to the temporary file
    • Close the target file
    • If successful, delete the target file and rename the temporary file.

    This is an expensive operation so think hard about whether you really need this if it is a frequent event.

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2011, 05:45
  2. Writing the text within a text edit to a file
    By Splatify in forum Newbie
    Replies: 4
    Last Post: 23rd February 2011, 23:48
  3. Replies: 0
    Last Post: 2nd August 2010, 10:37
  4. Replies: 3
    Last Post: 3rd May 2009, 09:58
  5. Replies: 1
    Last Post: 3rd September 2008, 15:16

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.