PDA

View Full Version : Problem with reading a file



Buhmann
15th February 2006, 22:31
Hi!
I'm using

QFile MyFile;
QString ConOut;

MyFile.setFileName("c:\test.txt");
MyFile.open(QIODevice::ReadOnly);

ConOut = MyFile.read(Q_INT64_C(1024));
to read the file containing

3 Terry Pratchett – Die Gelehrten der Scheibenwelt
4 Terry Pratchett – Rettet die Rundwelt – die gelehrten der Scheibenwelt 2
5 Terry pratchett – kleine freie männer
6 Christopher Moore - Bibel nach Biff
14 The SCHWA Corporation - SCHWA Weltbetriebsanleitung
15 Walter Moers - Die Stadt der Träumenden Bücher
16 Walter Moers - Rumo & Die Wunder im Dunkeln
17 Jens sparschuh – waldwärts
43 gdertg retgtrg - tdgtgthtrht
but what i get is

3 Terry Pratchett ? Die Gelehrten der Scheibenwelt
4 Terry Pratchett ? Rettet die Rundwelt ? die gelehrten der Scheibenwelt 2
5 Terry pratchett ? kleine freie männer
6 Christopher Moore - Bibel nach Biff
14 The SCHWA Corporation - SCHWA Weltbetriebsanleitung
15 Walter Moers - Die Stadt der Träumenden Bücher
16 Walter Moers - Rumo & Die Wunder im Dunkeln
17 Jens sparschuh ? waldwärts
43 gdertg retgtrg - tdgtgthtrht
As you can see some "-" are changed...wtf is this?! :S
Is there a better way to read files?


Buhmann

naresh
15th February 2006, 22:55
If you always use form "string - string" You can read text line by line like this:



QFile myFile("path");

if(myFile.open(QIODevice::ReadOnly))
{
while(!myFile.atEnd())
{
QString line=myFile.readLine();
QStringList array=line.split(" - ");
//now array[0] is everything before " - " and array[1] is everything after it
}
}

jacek
15th February 2006, 23:00
As you can see some "-" are changed...wtf is this?! :S
Is there a better way to read files?
Because some of those hyphens are not "-", but some other characters. Are you sure that this file is in pure ASCII? If not, use QTextStream and set proper encoding before reading its contents.

Buhmann
16th February 2006, 02:44
No the files are not always "string - string" and i'm not sure if they are pure ascii...i need some code that works on every file :S

jacek
16th February 2006, 02:53
i need some code that works on every file :S
You need to know what encoding to use as there are countless ways to interpret bytes. You could add a setting to your program configuration, to make user choose the encoding (by default its taken from locale).

Buhmann
16th February 2006, 14:55
You need to know what encoding to use as there are countless ways to interpret bytes. You could add a setting to your program configuration, to make user choose the encoding (by default its taken from locale).

Oh, I didn't know that... In fact I only want the bytes I guess. I don't want to do anything with the file, I just want it itself :P
In VB6 I only had to
Open "Path" For Binary As #F and then read it with
StrBuffer = String(10240, " ")
Get #F, , StrBuffer and everything worked fine...which function matches this in qt? :S

wysota
16th February 2006, 15:25
Try:
QString line=QString::fromLocal8Bit(myFile.readLine()); If you are lucky enough, it might work :)

Buhmann
16th February 2006, 15:52
Try:
QString line=QString::fromLocal8Bit(myFile.readLine()); If you are lucky enough, it might work :)


Thanks, but even if it works, I need 1024 bytes and not a line out of the file :S
I'm getting desperate. Why is my problem so hard to solve? I thought it must be simple and I'm just blind :S

wysota
16th February 2006, 16:13
So don't read a line but 1024 bytes. My point was to use QString::fromLocal8Bit(). QString has to know, how to convert raw bytes to unicode, you have to tell it how to do it.

elcuco
16th February 2006, 22:01
MyFile.setFileName("c:\test.txt");


how about:


MyFile.setFileName("c:\\test.txt");
MyFile.setFileName("c:/test.txt");


You know, you are backslashing "t"....

Buhmann
17th February 2006, 13:28
But that would mean there should be no input at all...but i got input, the problem is, that it's partly wrong :/
And the problem with fromLocal8Bit is, that it needs const char* and it seems that I'm too silly to get it run :S

jacek
17th February 2006, 14:02
But that would mean there should be no input at all...but i got input, the problem is, that it's partly wrong :/
If you try to compile your program with other compiler, you won't be able to open that file. It's a non-standard extension introduced by M$. In real C++ "\t" is interpreted as tab character, so under windows you should use "\\" for paths. AFAIR, you can also use "/" as Qt will change it to "\\".


And the problem with fromLocal8Bit is, that it needs const char* and it seems that I'm too silly to get it run :S
You can read that data to QByteArray and then use QByteArray::data() or use QTextStream::read() (and forget about QString::fromLocal8bit()).