Results 1 to 12 of 12

Thread: Reading a text file

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

    Default Reading a text file

    Hello

    In my Project I am creating a LogReport.txt. Which maintain a Report like below
    ECU->USS
    Sensor Time Data
    USS1 17:45:51 0x0,0xff,0x0,0x0
    USS2 17:45:52 0x0,0x0,0xff,0x0
    USS3 17:45:53 0x0,0x0,0x0,0x0
    USS4 17:45:54 0x0,0xff,0x0,0x0
    USS->ECU
    Sensor Time Data
    USS1 17:45:51 0x0,0x0,0x0,0x0
    USS2 17:45:52 0x0,0xff,0x0,0x0
    USS3 17:45:53 0x0,0xff,0x0,0x0
    USS4 17:45:54 0x0,0x0,0xff,0x0
    ------
    ------
    And so on until the code run.
    Now once I stop my code I have to read back same data on button click.
    Suppose on first click I have to read 17:45:51 0x0 0xff 0x0 0x0 and second click 17:45:52 0x0 0x0 0xff 0x0 and so on.
    Can any one let me know how can I read each line with five different column.

    Writefile
    Qt Code:
    1. file->setFileName("LogReport.txt");
    2. file->open(QIODevice::WriteOnly | QIODevice::Text);
    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 

  2. #2
    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


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

    Default Re: Reading a text file

    For readline it will read whole Line like
    Qt Code:
    1. QString line = in.readLine();
    2. qDebug()<<"line"<<line;
    To copy to clipboard, switch view to plain text mode 
    o/p:- "USS1 12:17:52 0x0 0x0 0x0 0x0"
    Now how can I extract each text and store in array.
    it is better if I read 12:17:52 ,0x0,0x0,0x0,0x0 separately every time.

  4. #4
    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

    So what is stopping you from doing that?
    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.


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

    Default Re: Reading a text file

    So I have button in my gui when it click I should read First line.
    Again if I click I should read second line and so on but
    when I click first time I am getting the first line
    but when clicking on second time I am getting nothing.

    Qt Code:
    1. QTextStream in(file);
    2. QString line = in.readLine();
    3. qDebug()<<"line"<<line
    To copy to clipboard, switch view to plain text mode 
    o/p:-USS1 15:14:33 0x0 0x0 0x0 0x5d (on first click)
    " "(on second click)

    It seem it is not going to second line

  6. #6
    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

    Show some concrete code please.
    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.


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

    Default Re: Reading a text file

    void ui_logging::Button_clicked()
    {

    QTextStream in(file);
    QTextStream stream(file);
    QString str1;


    for(int i = 0;i<6;i++)
    {

    in>>str1;
    qDebug()<<str1;

    }

    }
    O/p:First Click->USS1 15:14:33 0x0 0x0 0x0 0x5d
    2nd Click->"" "" "" "" "" ""
    3rd click->"" "" "" "" "" ""
    --------so on
    So on every click I have to read 6 string and on another click read the next 6 string but I am only getting the data on First click only and from
    next click getting Null

  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: Reading a text file

    What is file? Where do you open it? Why are you creating two text streams on it? Why do you do that every time the slot is called?
    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
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading a text file

    Qt Code:
    1. ui_logging::ui_logging()//constructor
    2. {
    3. QFile *file = new QFile();
    4. }
    5. openwrite_logfile() //call at start button
    6. {
    7. file->setFileName("LogReport.txt");
    8. file->open(QIODevice::WriteOnly | QIODevice::Text);
    9. }
    10. openread_logfile()//call at stop button
    11. {
    12. file->open(QIODevice::ReadOnly | QIODevice::Text);
    13. }
    14.  
    15. void ui_logging::Button_Clicked //display data on read button clicked
    16. {
    17.  
    18. QTextStream in(file);
    19. for(int i = 0;i<10;i++)
    20. {
    21.  
    22. in>>str1;
    23. qDebug()<<str1;
    24.  
    25. }
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    I don't have any idea to declare QTextStream in(file) as globally
    Last edited by anh5kor; 6th March 2015 at 09:44.

  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: Reading a text file

    You are aware 'file' variable from line #3 and the one from line #18 are different variables, right?

    One warning from me -- when asked for code if you ever again post pseudo-code then I will either ignore your post or give you a pseudo-answer comparable to your pseudo-code.
    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. #11
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading a text file

    I think till now you understand what exactly I am asking for.
    I have recognize the Problem and my problem is in below code
    Qt Code:
    1. void ui_logging::Button_Clicked()
    2. {
    3.  
    4. QTextStream in(file); //My Problem is here on every click I am creating the object again and again
    5.  
    6. for( i = 0;i<10;i++)
    7. {
    8.  
    9. in>>str1;
    10. qDebug()<<str1;
    11.  
    12. }
    13. int n = in.pos();
    14.  
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    Do you have any solution for the above.

  12. #12
    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

    The file is probably a class member variable. Make the stream a class member variable 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.


Similar Threads

  1. Qwtplot: reading data from text file
    By GG2013 in forum Newbie
    Replies: 0
    Last Post: 31st May 2013, 08:03
  2. Advice: Reading large text file.
    By enricong in forum Qt Programming
    Replies: 7
    Last Post: 16th July 2011, 13:11
  3. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 3rd May 2011, 00:47
  4. Reading from text file
    By jerkymotion in forum Qt Programming
    Replies: 5
    Last Post: 17th March 2011, 12:26
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.