PDA

View Full Version : Problem with reading from file



iamjayanth
23rd April 2009, 08:59
Hi,

I am trying to read from a file using readLine function. This is my code.

QFile file("tempfile");
if (!file.open(QIODevice::ReadOnly ))
{
return;
}
while (!file.atEnd())
{

QString line = file.readLine();
qWarning(line.toAscii());
}

But some characters in the read line is not printing like " ' " as it is . For example if the actual file content is this

str_cpy.c: In function ‘str_cpy’:

It is read and printed as

In function ‘str_cpy’:

What can I do to print these lines like the file content itself. What am I missing here.

Jayanth.S

spirit
23rd April 2009, 09:00
have a look at QTextStream, using it you can set needed encoding.

iamjayanth
23rd April 2009, 09:12
Thanks spirit for the quick reply. I tried QTextStream::setCodec() function to change to different character coding schemes. But none of them is working. I think characters like " ' " is causing the problem. Which is the encoding scheme that supports these characters.

Jayanth.S

Lykurg
23rd April 2009, 09:25
I think characters like " ' " is causing the problem. Which is the encoding scheme that supports these characters

Everyone! What encoding has your file. Make sure you use the same. Under Linux you can type under console
$ file yourfile.txt to get the encoding. And please show us, how you have used QTextStream.

iamjayanth
23rd April 2009, 10:55
QFile file("tempfile");
if (!file.open(QIODevice::ReadOnly ))
{
return;
}
QTextStream out(stdout);
out.setCodec("UTF-8");
while (!file.atEnd())
{
QString line = file.readLine();
line.remove(0,len);
if(line.contains("error",Qt::CaseSensitive)!=0)
{
out<<line;
list->addItem(line.toAscii());
}
else if(line.contains("warning",Qt::CaseSensitive)!=0)
{
out<<line;
list->addItem(line.toAscii().data());
}
else
{
out<<line;
list->addItem(line.toAscii().data());
}
}

This is how I used QText stream to read from a file ' tempfile ' and write the content to stdout and also to a list widget (list) . I tried file command in console for tempfile

$ file tempfile
tempfile: UTF-8 Unicode text

That is the reason I chose UTF-8 as codec for QTextStream. How to fix that problem.

iamjayanth
23rd April 2009, 10:57
QFile file("tempfile");
if (!file.open(QIODevice::ReadOnly ))
{
return;
}
QTextStream out(stdout);
out.setCodec("UTF-8");
while (!file.atEnd())
{
QString line = file.readLine();
line.remove(0,len);
if(line.contains("error",Qt::CaseSensitive)!=0)
{
out<<line;
list->addItem(line.toAscii());
}
else if(line.contains("warning",Qt::CaseSensitive)!=0)
{
out<<line;
list->addItem(line.toAscii().data());
}
else
{
out<<line;
list->addItem(line.toAscii().data());
}
}

This is how I used QText stream to read from a file ' tempfile ' and write the content to stdout and also to a list widget (list) . I tried file command in console for tempfile

$ file tempfile
tempfile: UTF-8 Unicode text

That is the reason I chose UTF-8 as codec for QTextStream. Still when I print the content of the file into stdout and listwidget some characters are not printing (" ' " for example).Thanks for the reply

Lykurg
23rd April 2009, 11:07
Use QTextStream for reading the file not only for giving the wrong read text out!

Edit: From the QFile documentation

QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
process_line(line);
}

iamjayanth
23rd April 2009, 11:32
Hi,

Now I am able to print to stdout correctly. But when I try to insert the line into QListWidget problem persists. Below given my code.

QFile file("tempfile");
if (!file.open(QIODevice::ReadOnly ))
{
return;
}
QTextStream in(&file);
QTextStream out(stdout);
in.setCodec("UTF-8");
while (!in.atEnd())
{
QString line = in.readLine();
out<<line<<'\n';
list->addItem(line.toAscii().data());
}

When I add the line to listwidget I am still getting problems. This time " ' " is printing as " ? ".

spirit
23rd April 2009, 11:33
Qt internally uses UTF-16.

iamjayanth
23rd April 2009, 11:35
Then how can I display the contents read from file in the listwidget as it is .

spirit
23rd April 2009, 11:43
try this


QByteArray encodedString = "...";
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString string = codec->toUnicode(encodedString);

read more about QTextCodec.

iamjayanth
23rd April 2009, 11:59
I think I have corrected the problem.... I used setUtf16() function to convert UTF-8 to UTF-16. I dont know if this is the correct way. But it worked.


list->addItem(line.setUtf16(0,line.length()));

Thanks spirit and lykurg for your valuable time. I am now reading QTextCodec to check for any better ways... Thanks again.

Lykurg
23rd April 2009, 12:03
while (!in.atEnd())
{
QString line = in.readLine();
out<<line<<'\n';
list->addItem(line.toAscii().data());
}

Please use the [ CODE ] tags and simply write:

while (!in.atEnd())
{
QString line = in.readLine();
out<<line<<'\n';
list->addItem(line); // <--
}
There is no need to transform the QString. And by using toAscii() - as you could expect - it is destroying the UTF-8 by converting to ascii...

iamjayanth
23rd April 2009, 12:41
hi,

So lykurg, you are saying the encoding was UTF-8 from the start. I just need to pass it onto listwidget. I think i have done the same earlear but didnt worked . But now it is working. The only difference is that I used normal file reading methods in the first time and QTextStream now. What is the difference in using QTextStream.

Lykurg
23rd April 2009, 13:54
So lykurg, you are saying the encoding was UTF-8 from the start. I just need to pass it onto listwidget. I think i have done the same earlear but didnt worked .

No you haven't. You have used the readLine directly on the QFile. And how the docs says: This function reads a line of ASCII characters from the device... So you tried to read UTF8 with a ASCII reader which leads to garbage in your QString. And setting garbage via your QString to the table widget won't heal the content.


But now it is working. The only difference is that I used normal file reading methods in the first time and QTextStream now. What is the difference in using QTextStream.

In QTextStream there is the difference. QTextStream respects the encoding of a file and do perform conversions on the bytes which it reads. So you get the right data in your QString.

iamjayanth
23rd April 2009, 14:12
Thanks Lykurg....That cleared it all....Thank you very much....