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?
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?