Results 1 to 12 of 12

Thread: Problem with reading a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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).

  3. #3
    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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

  5. #5
    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  7. #7
    Join Date
    Jan 2006
    Posts
    371
    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"....

  8. #8
    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

  9. #9
    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
  •  
Qt is a trademark of The Qt Company.