Results 1 to 12 of 12

Thread: Problem with reading a file

  1. #1
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with reading a file

    Hi!
    I'm using
    Qt Code:
    1. QFile MyFile;
    2. QString ConOut;
    3.  
    4. MyFile.setFileName("c:\test.txt");
    5. MyFile.open(QIODevice::ReadOnly);
    6.  
    7. ConOut = MyFile.read(Q_INT64_C(1024));
    To copy to clipboard, switch view to plain text mode 
    to read the file containing
    Qt Code:
    1. 3 Terry Pratchett – Die Gelehrten der Scheibenwelt
    2. 4 Terry Pratchett – Rettet die Rundwelt – die gelehrten der Scheibenwelt 2
    3. 5 Terry pratchett – kleine freie männer
    4. 6 Christopher Moore - Bibel nach Biff
    5. 14 The SCHWA Corporation - SCHWA Weltbetriebsanleitung
    6. 15 Walter Moers - Die Stadt der Träumenden Bücher
    7. 16 Walter Moers - Rumo & Die Wunder im Dunkeln
    8. 17 Jens sparschuh – waldwärts
    9. 43 gdertg retgtrg - tdgtgthtrht
    To copy to clipboard, switch view to plain text mode 
    but what i get is
    Qt Code:
    1. 3 Terry Pratchett ? Die Gelehrten der Scheibenwelt
    2. 4 Terry Pratchett ? Rettet die Rundwelt ? die gelehrten der Scheibenwelt 2
    3. 5 Terry pratchett ? kleine freie männer
    4. 6 Christopher Moore - Bibel nach Biff
    5. 14 The SCHWA Corporation - SCHWA Weltbetriebsanleitung
    6. 15 Walter Moers - Die Stadt der Träumenden Bücher
    7. 16 Walter Moers - Rumo & Die Wunder im Dunkeln
    8. 17 Jens sparschuh ? waldwärts
    9. 43 gdertg retgtrg - tdgtgthtrht
    To copy to clipboard, switch view to plain text mode 
    As you can see some "-" are changed...wtf is this?! :S
    Is there a better way to read files?


    Buhmann

  2. #2
    Join Date
    Feb 2006
    Location
    Warsaw, Poland
    Posts
    45
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with reading a file

    If you always use form "string - string" You can read text line by line like this:

    Qt Code:
    1. QFile myFile("path");
    2.  
    3. if(myFile.open(QIODevice::ReadOnly))
    4. {
    5. while(!myFile.atEnd())
    6. {
    7. QString line=myFile.readLine();
    8. QStringList array=line.split(" - ");
    9. //now array[0] is everything before " - " and array[1] is everything after it
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading a file

    Quote Originally Posted by Buhmann
    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.

  4. #4
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with reading a file

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading a file

    Quote Originally Posted by Buhmann
    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).

  6. #6
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with reading a file

    Quote Originally Posted by jacek
    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
    Qt Code:
    1. Open "Path" For Binary As #F
    To copy to clipboard, switch view to plain text mode 
    and then read it with
    Qt Code:
    1. StrBuffer = String(10240, " ")
    2. Get #F, , StrBuffer
    To copy to clipboard, switch view to plain text mode 
    and everything worked fine...which function matches this in qt? :S

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with reading a file

    Try:
    Qt Code:
    1. QString line=QString::fromLocal8Bit(myFile.readLine());
    To copy to clipboard, switch view to plain text mode 
    If you are lucky enough, it might work

  8. #8
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with reading a file

    Quote Originally Posted by wysota
    Try:
    Qt Code:
    1. QString line=QString::fromLocal8Bit(myFile.readLine());
    To copy to clipboard, switch view to plain text mode 
    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with reading a file

    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.

  10. #10
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading a file

    Qt Code:
    1. MyFile.setFileName("c:\test.txt");
    To copy to clipboard, switch view to plain text mode 

    how about:
    Qt Code:
    1. MyFile.setFileName("c:\\test.txt");
    2. MyFile.setFileName("c:/test.txt");
    To copy to clipboard, switch view to plain text mode 

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

  11. #11
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with reading a file

    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

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading a file

    Quote Originally Posted by Buhmann
    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 "\\".

    Quote Originally Posted by Buhmann
    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()).

Similar Threads

  1. cant open file newby problem
    By lmguru in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2009, 14:53
  2. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 12:48
  3. Replies: 7
    Last Post: 29th August 2008, 10:24
  4. Problem to find file path
    By phillip_Qt in forum Qt Programming
    Replies: 14
    Last Post: 28th April 2008, 10:06
  5. QProcess problem with windows batch file
    By bood in forum Qt Programming
    Replies: 11
    Last Post: 6th January 2006, 08:08

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.