Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ Why not use random access files?
|
Why not use random access files?
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Orioneyes
(2 posts) Bio
|
| Date
| Sat 17 Jun 2006 03:01 AM (UTC) |
| Message
| I run a SWR mud and noticed the file opening methods on all of them anew now that I've been learning C. I found out about random access files, and I've been wondering about them.
Why are all the files saved as text files instead of binaries? Is there some inherent flaw in this file type, or perhaps something special about the codebase I should know in relation to this file type? Just to let you know what I mean..
/* Vector structure */
struct vector
{
int x;
int y;
int z;
};
int main ()
{
struct vector vect;
/* We skip to record 6, for example */
fp = fopen("myFile.dat", "rb+");
/* We know the exact spot in the file using this formula */
fseek( fp, 5 * sizeof(struct vector), SEEK_SET );
/* At our position we read in the data en masse */
fread( &vect, sizeof( struct vect ), 1, fp );
printf("X: %d Y: %d Z: %d\n", vect.x, vect.y, vect.z );
fclose(fp);
return 0;
}
The code requires that you create a binary file before you can use it, but then again you can have a default size of 100 records and increase it whenever its 3/4 of the way full, for instance. Does anyone know why this isn't done? | | Top |
|
| Posted by
| Nick Gammon
Australia (23,166 posts) Bio
Forum Administrator |
| Date
| Reply #1 on Sat 17 Jun 2006 03:53 AM (UTC) |
| Message
| It's just a lot less flexible, that's why. On modern computers the speed and size of text files is hardly an issue. In your example if you wanted to add another field (int w for example) then the structure size changes, and the old data can no longer be accessed.
Also it doesn't lend itself to strings very well. For fixed-length records you need to allocate the longest possible size for a string, so what would you allow for a room description for instance? 1000 characters? Even then it would be too small for a few cases, and much too large for most of them, which is very wasteful.
It would also be tedious in the extreme to edit such a file "by hand" with a text editor to make corrections outside the program.
In fact, modern technique is to not only use text, but with named fields, so they are more easily expanded later, for example, XML, or things like Lua serialised data. For example:
roomname = "West Wing"
roomdesc = "You are standing within the expanse of the famous Darkhaven Square."
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Orioneyes
(2 posts) Bio
|
| Date
| Reply #2 on Sat 17 Jun 2006 04:41 PM (UTC) |
| Message
| | makes sense. Thanks Nick :) | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
10,685 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top