PDA

View Full Version : Reading from text file



jerkymotion
16th March 2011, 22:24
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::


void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
QFile file(filename);
if(file.open(QIODevice::ReadOnly | QIODevice::Text));
{
QTextStream in(&file);
//QString mm;

QString line;
//char *ptr;
// QByteArray ba;
while (!in.atEnd())
{
line = in.readLine(0);//reads whole text file and loads it to Qstring line
// ba=line.toLatin1();


//line.fromLatin1(ba,-1);
ui->textBrowser->setText(line);
}

for(int i=0;i<=line.length();++i)
{

// ba=mm.toLatin1();
//*ptr=ba.data();
// if(line.at(i)==QChar('s'))
//{
//line.append("ssss");
//}
QChar ch=line.at(i).toAscii();
int chvalue=ch.toAscii();
if(chvalue==48)
{
ui->lineEdit->setText(ch);
}
}

file.close();

}
}

schnitzel
17th March 2011, 00:11
You might want to learn how to use Qt Creator's debugger.

stampede
17th March 2011, 00:24
void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
QFile file(filename);
if(file.open(QIODevice::ReadOnly | QIODevice::Text));
{
QTextStream in(&file);
QString line;
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().

jerkymotion
17th March 2011, 10:02
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

void MainWindow::on_pushButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("wew"),"",tr("allfiles(*.txt)"));
QFile file(filename);
QString line;
if(file.open(QIODevice::ReadOnly | QIODevice::Text));
{
QTextStream in(&file);

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++)
{
QChar ch=line.at(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:)

stampede
17th March 2011, 10:19
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().

jerkymotion
17th March 2011, 12:26
thank u stampede ..........It really worked!!!!!!!!!!;)