PDA

View Full Version : how can i read a specific string from file?



sajastu
16th January 2015, 17:44
Hi,
i've written a code for writing data on binary file:


QByteArray ar=ui->User_signUp->text().toUtf8()+"%"+ ui->Pass_singUp->text().toUtf8()+"$";
QFile file("users.dat");
file.open(QIODevice::WriteOnly | QIODevice::Append );
file.write(ar);

pattern in binary file:


username0%password0&username1%password1& ...


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

d_stranz
16th January 2015, 18:07
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.

ChrisW67
16th January 2015, 20:02
You could use fixed length records and an index in a system called ISAM (http://en.m.wikipedia.org/wiki/ISAM).
Perhaps one of the dbm (http://en.m.wikipedia.org/wiki/Dbm) or Berkeley DB (http://en.m.wikipedia.org/wiki/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.

d_stranz
16th January 2015, 21:21
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.

wysota
16th January 2015, 23:17
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).

ChrisW67
16th January 2015, 23:30
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