Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!ukc!warwick!geoff From: geoff@cs.warwick.ac.uk (Geoff Rimmer) Newsgroups: comp.lang.c Subject: Re: binary data files Message-ID: <1821@ubu.warwick.UUCP> Date: 5 May 89 16:15:54 GMT References: <10946@bloom-beacon.MIT.EDU> <12546@ut-emx.UUCP> <8758@csli.Stanford.EDU> <11021@bloom-beacon.MIT.EDU> <14301@bfmny0.UUCP> <8815@csli.Stanford.EDU> Sender: news@warwick.UUCP Organization: Computer Science, Warwick University, UK Lines: 45 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