Re: Reading from text file
You might want to learn how to use Qt Creator's debugger.
Re: Reading from text file
Code:
void MainWindow::on_pushButton_clicked()
{
QString filename
= QFileDialog::getOpenFileName(this, tr
("wew"),
"",tr
("allfiles(*.txt)"));
{
while (!in.atEnd())
{
line = in.readLine(0);//reads whole text file and loads it to Qstring line
ui->textBrowser->setText(line);
}
for(int i=0;i<=line.length();++i)
{
QChar ch
=line.
at(i
).
toAscii();
int chvalue=ch.toAscii();
if(chvalue==48)
{
ui->lineEdit->setText(ch);
}
}
file.close();
}
}
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().
1 Attachment(s)
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
Code:
void MainWindow::on_pushButton_clicked()
{
QString filename
= QFileDialog::getOpenFileName(this, tr
("wew"),
"",tr
("allfiles(*.txt)"));
{
while (!in.atEnd())
{
line = in.readAll();//reads whole text file and loads it to Qstring line
ui->textBrowser->setText(line);
}
}
for(int i=0;i<line.length();i++)
{
int chvalue=ch.toAscii();//change to ascii value
if(chvalue==48)//if 0 is encountered here
{
ui->textBrowser_2->setText(ch);//set the character value in another textbowser
}
}
file.close();
}
my output on textbrowser_2 is only one 0.....please help me
my ouput look like this:)
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().
Re: Reading from text file
thank u stampede ..........It really worked!!!!!!!!!!;)