Results 1 to 6 of 6

Thread: how can i read a specific string from file?

  1. #1
    Join Date
    Jan 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default how can i read a specific string from file?

    Hi,
    i've written a code for writing data on binary file:
    Qt Code:
    1. QByteArray ar=ui->User_signUp->text().toUtf8()+"%"+ ui->Pass_singUp->text().toUtf8()+"$";
    2. QFile file("users.dat");
    3. file.open(QIODevice::WriteOnly | QIODevice::Append );
    4. file.write(ar);
    To copy to clipboard, switch view to plain text mode 
    pattern in binary file:
    Qt Code:
    1. username0%password0&username1%password1& ...
    To copy to clipboard, switch view to plain text mode 

    Now assume that i want to read a specific Stirng (Like username1 - i mean search a string) from file, i don't want to read whole file,because file may have huge data, for this i'm using "%" and "&" to know where an username or a password ends.
    do we have a solution for this?!

    BR

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how can i read a specific string from file?

    If your code is in charge of writing the file, then you know where you have written the information. Open the file, seek to that point, and read from there.

    If the strings you want to read are spread throughout the file, then you can reserve the first "x" bytes of the file to record an index. Maybe "x" is 1024, so your actual data starts at offset 1024 from the start of the file. Make the index so that each entry in the index contains the offset of a username (index 0 = 1024, index 1 = offset to next user name, etc.). When you open the file, the first thing you do is to read in the index.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how can i read a specific string from file?

    You could use fixed length records and an index in a system called ISAM.
    Perhaps one of the dbm or Berkeley DB variants could do the job for you (Values as leys).

    It would probably be easier to use the Qt Sqlite support with a simple table and index, and let something specifically designed for searching do the work for you.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how can i read a specific string from file?

    I started to suggest using a DB, but that seemed to be a much bigger overhead than the problem description warranted. If the data is highly structured, then a DB would be the way to go. If all that is needed is to know where user information is located within a loosely structured file, then an offset lookup table is a bit easier to implement. The description of what the overall goals are is pretty vague, so it is difficult to recommend a course of action.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: how can i read a specific string from file?

    I would suggest an ini file and QSettings Which of course doesn't make sense for gigabytes worth of data (unless one can spread it among multiple files).
    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
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how can i read a specific string from file?

    I read this,
    Now assume that i want to read a specific Stirng (Like username1 - i mean search a string) from file,
    As wanting to see if a certain string value appears in the file rather than identifying the nth value in the file.

    It depends quite a bit on unspecified characteristics of the problem. Sorted data could allow a binary search. Fixed length records could make finding value n trivial etc. It could all be moot if "huge data" is only a few megabytes and searches reasonably infrequent: just scanning the file may be a good option.

    As usual TMTOWTDI

Similar Threads

  1. XML, read file, string to enum.....
    By Speerfish in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2014, 10:34
  2. Replies: 2
    Last Post: 4th February 2014, 11:47
  3. Replies: 5
    Last Post: 15th July 2013, 10:36
  4. Read a specific line from a file
    By rleojoseph in forum Qt Programming
    Replies: 11
    Last Post: 21st March 2011, 11:58
  5. setText using a string read from file
    By Splatify in forum Newbie
    Replies: 3
    Last Post: 28th October 2010, 15:39

Tags for this Thread

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.