binary data files

Geoff Rimmer geoff at cs.warwick.ac.uk
Sat May 6 02:15:54 AEST 1989


Let me throw in my 2c worth.

My "rules" for choosing whether I use a binary file, or an ASCII file
for storing data are as follows:

(1)	If I am storing a file of structs, I always use binary.  This
is because it is faster, and because it makes the code easier to write
and understand.

For example, to delete a struct whose "ref" is set to 999, and write
the new file of structs out elsewhere:

	struct blurfl buf;
	while ( fread ( (char*) &buf, sizeof(struct blurfl), 1, fpr))
	{
		if (buf->ref != 999)
			fwrite( (char*) &buf, sizeof(struct blurfl), 1, fpw);
	}

(2)	If I don't know how many fields will be read in at a time,
ie. one of the fields might determine how many more fields to be read in. 

With (2), I often use strtok(), which I've found very useful:

	while (!feof(fpr))
	{
		char *ptr, str[BUFSIZ];
		if (!fgets(str,BUFSIZ-1,fpr))
			break;
		if (!(ptr=strtok(str," \t")))
			continue;
		do
		{
			printf("%s\n",ptr);
		} while (ptr=strtok((char*)0," \t"));
	}

(In reality, I wouldn't use fgets, since it requires a maximum length
to read, which could result in lost data if there is a particularly
long line.)

I don't believe you can say "always use ascii files" - it just ain't
good enough for some applications.

Geoff



More information about the Comp.lang.c mailing list