Results 1 to 10 of 10

Thread: Reading a text file from end

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Reading a text file from end

    Hello,

    Please let me how can I read a text file from last line to first line.
    or is there any function in Qt which can I use in my code.

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

    Default Re: Reading a text file from end

    How big is the file? 10 lines, 10000 lines, or 10 billion lines?

  3. #3
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading a text file from end

    It's a dynamic size File it can be 10 lines,10000lines or 10 billion lines also
    Size is not fixed.

  4. #4
    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: Reading a text file from end

    Generally a file is an "unidirectional device" i.e. it can read/write data only in one direction from begin to end.

  5. #5
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading a text file from end

    There is no way that I can read file from end to begin because this
    the requirement for my Project.

  6. #6
    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: Reading a text file from end

    Say some more about this file. Is the structure of the file somehow ordered? Lines have a fixed length or variable?

  7. #7
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading a text file from end

    ok
    Actually in my GUI I have two button connect and disconnect on connect it will start writing to file and on disconnect it will stop.
    I have the Structure also
    Below is the code for Write File
    Qt Code:
    1. void ui_logging::log_report()
    2. {
    3. QTextStream stream(file);
    4. int index = 0;
    5.  
    6. stream << "ECU->USS\n";
    7. stream <<"Sensor"<<" "<<"Time"<<" "<<"Data"<<endl;
    8. for(int i = 0;i<12;i++)
    9. {
    10. stream << "USS"<<i+1<<" ";
    11.  
    12. stream<<time_log_ecu2uss[i];
    13. if(i<9)
    14. {
    15. stream<<" ";
    16. }
    17. else
    18. {
    19. stream<<" ";
    20. }
    21.  
    22. for(int j = 0;j<4;j++)
    23. {
    24.  
    25. stream<<("0x"+QString::number(Data_ecu2uss[index],16));
    26. if(j<3)
    27. {
    28. stream<<" ";
    29. }
    30. index++;
    31. }
    32.  
    33. stream<<endl;
    34. }
    35.  
    36. index = 0 ;
    37. stream << "USS->ECU\n";
    38. stream <<"Sensor"<<" "<<"Time"<<" "<<"Data"<<endl;
    39. for(int i = 0;i<12;i++)
    40. {
    41. stream << "USS"<<i+1<<" ";
    42. stream<<time_log_uss2ecu[i];
    43. if(i<9)
    44. {
    45. stream<<" ";
    46. }
    47. else
    48. {
    49. stream<<" ";
    50. }
    51. for(int j = 0;j<4;j++)
    52. {
    53. stream<<("0x"+QString::number(Data_uss2ecu[index],16));
    54. if(j<3)
    55. {
    56. stream<<" ";
    57. }
    58. index++;
    59. }
    60. stream<<endl;
    61. }
    62.  
    63. stream<<"******************************************************************"<<endl;
    64.  
    65. }
    To copy to clipboard, switch view to plain text mode 
    Where my file Look like below
    ECU->USS
    Sensor Time Data
    USS1 12:25:15 0x0 0x0 0x0 0x0
    USS2 12:25:15 0x0 0x0 0x0 0x0
    USS3 12:25:15 0x0 0x0 0x0 0x0
    USS4 12:25:15 0x0 0x0 0x0 0x0
    USS5 12:25:15 0x0 0x0 0x0 0x0
    USS6 12:25:15 0x0 0x0 0x0 0x0
    USS7 12:25:15 0x0 0x0 0x0 0x0
    USS8 12:25:15 0x0 0x0 0x0 0x0
    USS9 12:25:15 0x0 0x0 0x0 0x0
    USS10 12:25:15 0x0 0x0 0x0 0x0
    USS11 12:25:15 0x0 0x0 0x0 0x0
    USS12 12:25:15 0x0 0x0 0x0 0x0
    USS->ECU
    Sensor Time Data
    USS1 12:25:15 0x0 0x0 0x0 0x0
    USS2 12:25:15 0x0 0x0 0x0 0x0
    USS3 12:25:15 0x0 0x0 0x0 0x0
    USS4 12:25:15 0x0 0x0 0x0 0x0
    USS5 12:25:15 0x0 0x0 0x0 0x0
    USS6 12:25:15 0x0 0x0 0x0 0x0
    USS7 12:25:15 0x0 0x0 0x0 0x0
    USS8 12:25:15 0x0 0x0 0x0 0x0
    USS9 12:25:15 0x0 0x0 0x0 0x0
    USS10 12:25:15 0x0 0x0 0x0 0x0
    USS11 12:25:15 0x0 0x0 0x0 0x0
    USS12 12:25:15 0x0 0x0 0x0 0x0
    ************************************************** ****************
    ECU->USS
    Sensor Time Data
    USS1 12:25:16 0x0 0x0 0x0 0x0
    USS2 12:25:16 0x0 0x0 0x0 0x0
    USS3 12:25:16 0x0 0x0 0x0 0x0
    USS4 12:25:16 0x0 0x0 0x0 0x0
    USS5 12:25:16 0x0 0x0 0x0 0x0
    USS6 12:25:16 0x0 0x0 0x0 0x0
    USS7 12:25:16 0x0 0x0 0x0 0x0
    USS8 12:25:16 0x0 0x0 0x0 0x0
    USS9 12:25:16 0x0 0x0 0x0 0x0
    USS10 12:25:16 0x0 0x0 0x0 0x0
    USS11 12:25:16 0x0 0x0 0x0 0x0
    USS12 12:25:16 0x0 0x0 0x0 0x0
    USS->ECU
    Sensor Time Data
    USS1 12:25:16 0x0 0x0 0x0 0x0
    USS2 12:25:16 0x0 0x0 0x0 0x0
    USS3 12:25:16 0x0 0x0 0x0 0x0
    USS4 12:25:16 0x0 0x0 0x0 0x0
    USS5 12:25:16 0x0 0x0 0x0 0x0
    USS6 12:25:16 0x0 0x0 0x0 0x0
    USS7 12:25:16 0x0 0x0 0x0 0x0
    USS8 12:25:16 0x0 0x0 0x0 0x0
    USS9 12:25:16 0x0 0x0 0x0 0x0
    USS10 12:25:16 0x0 0x0 0x0 0x0
    USS11 12:25:16 0x0 0x0 0x0 0x0
    USS12 12:25:16 0x0 0x0 0x0 0x0
    ************************************************** ****************
    It will write until and unless I stop the button
    Now I have two button called Read_up2down which will read the data from USS1 to USS12 on every click and Read_down2up which will read the data from USS12 to USS1 and rest element has to be ignored.
    I am able to read data from USS1 to USS12 but not from USS12 to USS1
    Below is the code for Read_up2down
    Qt Code:
    1. void ui_logging::Read_up2down()
    2. {
    3. count++;
    4.  
    5. QTextStream in(file);
    6. //get position of file
    7. if(!flag_Position)
    8. {
    9. in.seek(n);
    10. }
    11.  
    12. for( i = 0;i<4;i++)//Ignore the unused data
    13. {
    14.  
    15. in>>str1;
    16. qDebug()<<str1;
    17. }
    18. for(int i = 0;i<72;i++)//Get the used data first time
    19. {
    20. in>>str[i];
    21. qDebug()<<str[i];
    22.  
    23. }
    24. if(count == 2)//ignore the *** every time
    25. {
    26. in>>str1;
    27. qDebug()<<str1;
    28. count = 0;
    29. }
    30.  
    31. n = in.pos();
    32. in.skipWhiteSpace();
    33. flag_Position = false;
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    O/p:
    1st Click
    "ECU->USS"
    "Sensor"
    "Time"
    "Data"
    "USS1"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS2"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS3"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS4"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS5"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS6"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS7"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS8"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS9"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS10"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS11"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS12"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    2nd Click
    "USS->ECU"
    "Sensor"
    "Time"
    "Data"
    "USS1"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS2"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS3"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS4"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS5"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS6"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS7"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS8"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS9"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS10"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS11"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "USS12"
    "12:25:15"
    "0x0"
    "0x0"
    "0x0"
    "0x0"
    "************************************************* *****************"
    and so on...

  8. #8
    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: Reading a text file from end

    Maybe using some SQL database (e.g. SQLite) is a solution ?

  9. #9
    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: Reading a text file from end

    To read a file from the end seek to the end of file, seek back a defined number of bytes (e.g. 4kB to match the memory page size), read those 4kB into a buffer, scan the buffer character by character starting from the end for a newline. When found, this marks the end of your previous-to-last line and the last line is everything past it. Process and repeat. If there is no newline, read another chunk from from the file and carry on.
    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.


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

    Default Re: Reading a text file from end

    Anh5kor, it turns outs, does not actually want to read the file backward line by line. Rather, the desire is to read multiline blocks in reverse order from the file, but read the content of each block in a forward direction.

    An adaptation of wysota's approach that looks backward for "ECU->USS\n" rather than a lone newline should provide the place to start reading a block from.

    With potentially billions of lines/blocks then a fixed block size (binary or text), a separately written index file of block start offsets, or storage in a database, would also be options.

Similar Threads

  1. Reading a text file
    By anh5kor in forum Newbie
    Replies: 11
    Last Post: 6th March 2015, 10:14
  2. Qwtplot: reading data from text file
    By GG2013 in forum Newbie
    Replies: 0
    Last Post: 31st May 2013, 07:03
  3. Advice: Reading large text file.
    By enricong in forum Qt Programming
    Replies: 7
    Last Post: 16th July 2011, 12:11
  4. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 2nd May 2011, 23:47
  5. Reading from text file
    By jerkymotion in forum Qt Programming
    Replies: 5
    Last Post: 17th March 2011, 11:26

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.