Results 1 to 6 of 6

Thread: Reading from text file

  1. #1
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Reading from text file

    I want to load the data from text file ..and i successfully did it by loading it in textbrowser....Now I want to load the specific characters into line edit whenever I encounter 0 in string of text file .....I succededd here but the program shows errror
    Error message:::The application has requested the runtime to terminate in a unusual way
    Please contact the application support team for more information
    My Code::
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
    4. QFile file(filename);
    5. if(file.open(QIODevice::ReadOnly | QIODevice::Text));
    6. {
    7. QTextStream in(&file);
    8. //QString mm;
    9.  
    10. QString line;
    11. //char *ptr;
    12. // QByteArray ba;
    13. while (!in.atEnd())
    14. {
    15. line = in.readLine(0);//reads whole text file and loads it to Qstring line
    16. // ba=line.toLatin1();
    17.  
    18.  
    19. //line.fromLatin1(ba,-1);
    20. ui->textBrowser->setText(line);
    21. }
    22.  
    23. for(int i=0;i<=line.length();++i)
    24. {
    25.  
    26. // ba=mm.toLatin1();
    27. //*ptr=ba.data();
    28. // if(line.at(i)==QChar('s'))
    29. //{
    30. //line.append("ssss");
    31. //}
    32. QChar ch=line.at(i).toAscii();
    33. int chvalue=ch.toAscii();
    34. if(chvalue==48)
    35. {
    36. ui->lineEdit->setText(ch);
    37. }
    38. }
    39.  
    40. file.close();
    41.  
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading from text file

    You might want to learn how to use Qt Creator's debugger.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Reading from text file

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
    4. QFile file(filename);
    5. if(file.open(QIODevice::ReadOnly | QIODevice::Text));
    6. {
    7. QTextStream in(&file);
    8. QString line;
    9. while (!in.atEnd())
    10. {
    11. line = in.readLine(0);//reads whole text file and loads it to Qstring line
    12. ui->textBrowser->setText(line);
    13. }
    14.  
    15. for(int i=0;i<=line.length();++i)
    16. {
    17. QChar ch=line.at(i).toAscii();
    18. int chvalue=ch.toAscii();
    19. if(chvalue==48)
    20. {
    21. ui->lineEdit->setText(ch);
    22. }
    23. }
    24. file.close();
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Looks better now, doesn't it ?
    I don't know if you intend to do it this way, but you are testing for '0' only in last line of the file ( check the value of "line" after while loop ).
    If you want to find a character in the string, you can use QString::indexOf method. Also there is no need to hardcode the ascii value for zero ( 48 is for '0' in your code, right ? ) and doing the QChar manipulations, line.indexOf("0") will work as expected.
    Read QString documentation, it's full of useful informations.

    edit:
    about the crash - you should read documentation for QString::at().

  4. #4
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading from text file

    I have sucessfully read the whole text file(.txt file) in my text browser
    and now I want to display all the 0's in text file to another textbowser_2
    but my program only display one 0 in the textbrowser...
    my text file content looks like this:
    11110000
    11110000
    2222aaa
    bbbb
    and my code
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
    4. QFile file(filename);
    5. QString line;
    6. if(file.open(QIODevice::ReadOnly | QIODevice::Text));
    7. {
    8. QTextStream in(&file);
    9.  
    10. while (!in.atEnd())
    11. {
    12. line = in.readAll();//reads whole text file and loads it to Qstring line
    13. ui->textBrowser->setText(line);
    14. }
    15. }
    16. for(int i=0;i<line.length();i++)
    17. {
    18. QChar ch=line.at(i);
    19. int chvalue=ch.toAscii();//change to ascii value
    20. if(chvalue==48)//if 0 is encountered here
    21. {
    22.  
    23. ui->textBrowser_2->setText(ch);//set the character value in another textbowser
    24. }
    25.  
    26. }
    27. file.close();
    28. }
    To copy to clipboard, switch view to plain text mode 

    my output on textbrowser_2 is only one 0.....please help me

    my ouput look like this
    Attached Images Attached Images

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Reading from text file

    Read my previous post again - you are searching for zeros only in last line of the file, so put the "for" loop inside "while" loop.
    Another thing is that if you call textBrowser2->setText(), it will clear previous content. Try with textBrowser_2->append().

  6. The following user says thank you to stampede for this useful post:

    jerkymotion (17th March 2011)

  7. #6
    Join Date
    Mar 2011
    Posts
    35
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading from text file

    thank u stampede ..........It really worked!!!!!!!!!!

Similar Threads

  1. Reading text from a PlainTextEdit
    By Bertie in forum Newbie
    Replies: 5
    Last Post: 9th March 2011, 14:24
  2. Access violation when reading big text files
    By robertson1 in forum General Programming
    Replies: 0
    Last Post: 18th September 2008, 06:59
  3. Replies: 1
    Last Post: 3rd September 2008, 14:16
  4. problem with reading text files
    By Axsis in forum Newbie
    Replies: 1
    Last Post: 25th April 2008, 12:29
  5. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55

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.