Results 1 to 8 of 8

Thread: Erasing the content of a txt file

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Erasing the content of a txt file

    Hello!

    I'm trying to create a function in my software that creates new txt files with increasing number each time a new file must be created.

    In other words, normally we would use a simple variable (int) that would be increased each time a new txt file must be created, and that would be read each time to see the new name. But the problem is that the software may be shut down, and when startet again, the int variable would be set to zero once again instead of continuing from the last number.

    The algorithm I decided to create is this: the software opens a different txt file "TxtNumber" with a given number on it (in the beginning, 0). It reads than that number, put it in a variable and add +1 (so the first txt will be "Text number 1"). Than it erases the TxtNumber and write on it the new number (1). And from that it moves one.

    If the software is shut down and open again, when it reads the "TxtNumber", it will read "1", add +1, erase and write "2" in it.


    The problem is that I couldn't find a function that can erase the txt file (before I write the new number on it) or, given the fact that just one line will be used, erase one line. Is there such function?


    Thanks!

  2. #2
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Erasing the content of a txt file

    You can simply QIODevice::seek after you read the integer variable.

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Erasing the content of a txt file

    Quote Originally Posted by cincirin View Post
    You can simply QIODevice::seek after you read the integer variable.
    Sorry, I didn't understand how to do that. Could you please be a little bit more descriptive? (e.g. with a code?)

    Tanks!

  4. #4
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Erasing the content of a txt file

    Actually, you probably should -- instead of raw reading and writing files -- use QSettings, which is a class well suited to keeping track of such information, including any metadata related to the files.

  5. #5
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Erasing the content of a txt file

    If you don't read/write any other informations in file, then, as @mvuori said, QSettings is what you should looking for.
    Else if you want to use a particular file, before you read the integer variable, you can find the position in file with QIODevice :: pos. After you read a integer variable from file, increment it, and call seek() with position that you have found. Now you can write new incremented variable. Note, that this will be working if you open the file in binary mode.

  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: Erasing the content of a txt file

    I would not recommend storing the count separately; the potential for things to get out of sync is non-zero, and if the external count is ever lost, you can't recover.

    I'd simply store the count as the first item in the file. Set aside 4 bytes at position 0 and write a binary unsigned integer to it. Or set aside, say, 10 bytes and store the ASCII representation of an integer there. Retrieve when the file is next read, and update as required, which only needs a seek(0) and a read() or write(). The contents that follow are exactly the same as before, but offset by the length of the header. Everything is now self-contained.

  7. #7
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Erasing the content of a txt file

    If you really need to keep a count like this, vs just using one of the "find an unused file name" schemes, use a SQL database. Overkill, I know, for such a simple function, but simple and reliable to accomplish.

  8. #8
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Erasing the content of a txt file

    Hello!

    First, thanks for the help. In fact I was able to find a solution to the problem without having to ask for any "delete txt" function, despite I still don't know exactly what Qt is thinking when it does so with the code I implemented. Here it is:

    Qt Code:
    1. void myclient::definetxtpath()
    2. {
    3. v_txtdirectory = new QDir;
    4. v_txtdirectoryname = "./LifeshockGraph";
    5. if (!v_txtdirectory->exists(v_txtdirectoryname))
    6. {
    7. v_txtdirectory->mkpath(v_txtdirectoryname);
    8. Filenumber = "./LifeshockGraph/ECGnumber.txt";
    9. }
    10. else
    11. {
    12. Filenumber = "./LifeshockGraph/ECGnumber.txt";
    13. }
    14. }
    15.  
    16. void myclient::definetxttitle()
    17. {
    18. mFileN.setFileName(Filenumber);
    19. if (!mFileN.exists())
    20. {
    21. //Write initial number
    22. if(!mFileN.open(QFile::WriteOnly | QFile::Text))
    23. {
    24. qDebug() << "Could not open number file for writing initial number.";
    25. pmw->ui->cmd->append("Could not open number file for writing initial number");
    26. return;
    27. }
    28. else
    29. {
    30. outN = new QTextStream(&mFileN);
    31. *outN << v_currentnumber; //Escreve "0".
    32. mFileN.flush();
    33. mFileN.close();
    34. }
    35. }
    36.  
    37. //Read previous ECG number
    38. mFileN.setFileName(Filenumber);
    39. if(!mFileN.open(QFile::ReadOnly | QFile::Text))
    40. {
    41. qDebug() << "Could not open number file for reading previous number.";
    42. pmw->ui->cmd->append("Could not open number file for reading previous number");
    43. v_currentdatetimeFailure = true;
    44. return;
    45. }
    46. else
    47. {
    48. inN = new QTextStream(&mFileN);
    49. v_readint = inN->readLine();
    50.  
    51. v_currentnumber = v_readint.toInt();
    52. v_newnumber = v_currentnumber + 1;
    53.  
    54. mFileN.flush();
    55. mFileN.close();
    56.  
    57. //Write new ECG number
    58. mFileN.setFileName(Filenumber);
    59. if(!mFileN.open(QFile::WriteOnly | QFile::Text))
    60. {
    61. qDebug() << "Could not open number file for writing.";
    62. pmw->ui->cmd->append("Could not open number file for writing");
    63. return;
    64. }
    65. else if (v_currentdatetimeFailure==false) //Se ele conseguiu ler o número anterior, escreve o novo número.
    66. {
    67. outN = new QTextStream(&mFileN);
    68. *outN << v_newnumber;
    69. }
    70. else //Se tiver houvido erro na hora de ler o número anterior do ECG, dá erro.
    71. {
    72. qDebug() << "Complete failure in reading the ECG number.";
    73. pmw->ui->cmd->append("Complete failure in reading the ECG number");
    74. }
    75. }
    76.  
    77. mFileN.flush();
    78. mFileN.close();
    79.  
    80. Filename = "./LifeshockGraph/ECGtransmission.txt";
    81.  
    82. //Set txt title
    83. mFile.setFileName(Filename);
    84. if(!mFile.open(QFile::WriteOnly | QFile::Text))
    85. {
    86. qDebug() << "Could not open file for writing.";
    87. pmw->ui->cmd->append("Could not open file for writing");
    88. return;
    89. }
    90. else
    91. {
    92. out = new QTextStream(&mFile);
    93.  
    94. v_currentdatetime = QDateTime::currentDateTime();
    95. if (v_currentdatetimeFailure==false) *out << "Socket connection number " << v_newnumber << ".\n";
    96. else *out << "Failed to pick ECG transmission number.\n";
    97. *out << v_currentdatetime.toString("dd-MM-yyyy hh:mm:ss") << ".\n\n";
    98. }
    99. }
    To copy to clipboard, switch view to plain text mode 

    Quite a giant code...

    By the other hand, I possibly will one day improve my code and try the ways you showed to me, so I'still glad for the information.

    Lots of thanks.

    Martin/Momergil

Similar Threads

  1. encrypt the content of a file
    By franco.amato in forum Qt Programming
    Replies: 9
    Last Post: 11th May 2011, 16:35
  2. Erasing on transparent widget
    By mooklekloon in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2010, 16:53
  3. set content(&file)
    By rk0747 in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2010, 08:31
  4. How to modify the content of text file
    By grsandeep85 in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2009, 10:23
  5. Draw New data, without erasing old ones
    By linuxdev in forum Newbie
    Replies: 11
    Last Post: 7th January 2009, 08:34

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.