Results 1 to 8 of 8

Thread: Function for repositioning the file pointer

  1. #1
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Function for repositioning the file pointer

    In my app I have to save data in an external binary file (I choose QDataStream class); in particular, I have array of 20 items arranged in 100 memories. Like a bidimensional array, in which I would write o read data starting not necessarily from the begin of file.
    The following is the Read() function I'm developing:

    void prova12::Read()
    {
    QString miodato[230]; // array based on QString class
    QFile DataStore("store.dat"); // the external binary file to be read is: store.dat
    DataStore.open(QIODevice::ReadOnly; // file open in readonly mode
    QDataStream in(&DataStore); // datastream is read
    for(index = 0; index < 230; index++)
    in >> miodato[index]; // the first 230 data in store.dat are saved in "miodato" array
    }

    From the Qt help, I found:
    bool QIODevice::seek(qint64 pos)
    It seems to be the solution to my problem, when I want to start my reading from 3rd data; the following is my trial:

    void prova12::Read()
    {
    QString miodato[230];
    QFile DataStore("store.dat");
    DataStore.open(QIODevice::ReadOnly);
    QDataStream in(&DataStore);
    /****************/
    DataStore.seek(3); // Start to read from 3rd data
    /****************/
    for(index = 0; index < 230; index++)
    in >> miodato[index];
    }

    But, unfortunately, it doesn't work.
    Is there anybody that already use these instructions or understand what's my error?
    Thanks
    Paolo

  2. #2
    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: Function for repositioning the file pointer

    How does "doesn't work" manifest itself?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    7
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Function for repositioning the file pointer

    Your code would work if the strings start at the third byte in the file. I suspect what you're actually trying to do is start reading from the third string that was written, which will not work and cannot be done with QDataStream, but it's not entirely clear. Further information about how your data is being written would be useful.

  4. #4
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Function for repositioning the file pointer

    @Dan
    Yes you're right; I'm trying to read starting from my third string and not from the third byte.
    Is it possible to to this in your opinion?
    Thanks
    Paolo

  5. #5
    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: Function for repositioning the file pointer

    You have to read from the beginning as you don't know how long each string is. Alternatively use an sqlite database file, then you'll have records and will be able to jump to each record immediately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Function for repositioning the file pointer

    Or a structured file like an xml file

  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: Function for repositioning the file pointer

    Quote Originally Posted by tbscope View Post
    Or a structured file like an xml file
    Technically speaking with an xml file he'll still have to read the file from the beginning
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Apr 2010
    Posts
    9
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Function for repositioning the file pointer

    Solved!
    The Qdatastream.seek() function works perfectly if I set the pointer correctly; as my array elements are QString based, I've to look for the initial byte of 3rd string (so number is not "3" but depends on the String lenght).
    Thank you to all for your anwser.
    Paolo

Similar Threads

  1. Replies: 14
    Last Post: 1st December 2009, 21:45
  2. setting background color and repositioning
    By mind_freak in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2009, 07:22
  3. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 13:53
  4. Replies: 3
    Last Post: 25th May 2007, 08:49
  5. Disable QTextCursor Mouse click repositioning
    By VireX in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2007, 09: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.