Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

How do you seeking a position of a filestream without knowing it?

Extreme Coder - Delphi
Loyal Member
Joined
Sep 8, 2007
Messages
1,381
Reaction score
39
Hey guys, I'm basically writing untyped data into a single file and adding them sequentially.
For example
Writebuffer(VersionID,Sizeof(versionID); // will write the version id of the file
len1 := lengthof(string1);
Writebuffer(len1,sizeof(len1)); // will write the size of the length of the string so we can read it
Writebuffer(string1,len1); // will write the string1 to the file

// so if I happen to add multiple file types say I add bitmaps and videofiles and other data types, to read them would be easy I would just read the size of it first then read the datatype but for this to happen I would have to read the file sequentially for example :

Readbuffer(versionid,sizeof(versionID); // reads the first thing in the file since we know the structure
the position moves 4 bytes because versionID is an Int which is 4bytes of data so there fore the filstream.position = 4 right now.

So what I'm wondering is it possible to skip to a particular part of the filestream without having to read everything in a sequencing order? But for that we need to know the size in bytes right? what would be the easiest way to do this ?
 
Elite Diviner
Joined
Mar 30, 2013
Messages
456
Reaction score
42
Well, the easiest way would be to simply read everything to that point, then disregard it. In general though, you should be using Seekg.



Could be a lazy mofo, not sure if it would work in your case, and run a is ignore.

istream& ignore (streamsize n = 1, int delim = EOF);




But as said, I think SeekG is more what you're looking for.

Good luck!
 
Junior Spellweaver
Joined
Oct 27, 2008
Messages
165
Reaction score
89
if you hve multiple big data types, add a header to your file that will specify the file poisition of the data in the file.
 
Watching from above
Legend
Joined
Apr 9, 2004
Messages
3,828
Reaction score
752
If possible, you could also first gather all your data and write their positions-to-be at the beginning of the file and then continuously write the actual data at the end. So the offsets at the beginning would be just like memory pointers, but pointers to locations in a file. Then you wouldn't need to sequentially read all of the file but only the pointers section.

Actually this would also work in reverse: keep writing into the file first but save the starting offset for each of the writes as you go. When closing the file, write the pointers section at the end of the file.
 
Joined
May 17, 2007
Messages
2,474
Reaction score
681
Tracking the index by adding it to the beginning (best if you do not alter the file, as altering the header requires rewriting the whole file) or end of the file (as mentioned above). Alternatively (if you're always changing the file), you can keep track of the indexes in a separate file dedicated solely for indexing purposes.

If you don't want to do the above, add a string (name of the file?) along with the length before the file data. Once you've done that, start reading the file, read the string (name), if it matches the one you want, read the length and then read the data. If it doesn't match, read the length, and seek/skip that many bytes, and read the next string.
 
Extreme Coder - Delphi
Loyal Member
Joined
Sep 8, 2007
Messages
1,381
Reaction score
39
Tracking the index by adding it to the beginning (best if you do not alter the file, as altering the header requires rewriting the whole file) or end of the file (as mentioned above). Alternatively (if you're always changing the file), you can keep track of the indexes in a separate file dedicated solely for indexing purposes.

If you don't want to do the above, add a string (name of the file?) along with the length before the file data. Once you've done that, start reading the file, read the string (name), if it matches the one you want, read the length and then read the data. If it doesn't match, read the length, and seek/skip that many bytes, and read the next string.

The reading string and length and skip, until matches is exactly what im looking for. I'm surprised I didn't think of this, its genius haha. But instead i'm going to use integers because they are a fixed size of 4 bytes, that way i can id each file by 1,2,3,4,5 since for my case im loading images. Unfortunately the strings do not have a fixed size so I'd have to write the length of the string as well. Thanks guys for the suggestions !
 
Joined
May 17, 2007
Messages
2,474
Reaction score
681
The reading string and length and skip, until matches is exactly what im looking for. I'm surprised I didn't think of this, its genius haha. But instead i'm going to use integers because they are a fixed size of 4 bytes, that way i can id each file by 1,2,3,4,5 since for my case im loading images. Unfortunately the strings do not have a fixed size so I'd have to write the length of the string as well. Thanks guys for the suggestions !

an integer is much more efficient than a variable-sized string. Even a 1-byte integer is good if you have less than 257 entries!
 
Extreme Coder - Delphi
Loyal Member
Joined
Sep 8, 2007
Messages
1,381
Reaction score
39
Ok now I have another problem, Say I have 6 images in the one file, if I want to delete the Image and Index 2 how would I go about doing that?
 
• ♠️​ ♦️ ♣️ ​♥️ •
Joined
Mar 25, 2012
Messages
909
Reaction score
464
Ok now I have another problem, Say I have 6 images in the one file, if I want to delete the Image and Index 2 how would I go about doing that?

Unfortunately changing the file length (except for appending data on the end) results in rewriting the whole thing. You could, however, replace the affected data by 0's, that can be ignored by your seek function.
 
Back
Top