delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/08/18:48:06

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: File signature
Date: 6 May 1997 13:53:02 GMT
Organization: Oxford University, England
Lines: 33
Message-ID: <5knd3u$dnb@news.ox.ac.uk>
References: <5kn64n$qbe AT news DOT interlog DOT com>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gautam N. Lad (gautam AT interlog DOT com) wrote:
: Hi,
: I want to dump some binary data to a file, but before I do, I want the very first 
: string in each file to be something I choose.  What I want is something 
: similar to the GIF87a /GIF89a string in a CompuServe gif file.  So, what would 
: be the easiest way to do this.  I'll be using fwrite() to write the data, in binary 
: write mode.

Use a constant string ID length, for example 6, and just write the
characters in the string, i.e.:

fwrite("GNL97a",6,1,fp);

This writes G,N,L,9,7,a to the first 6 bytes of the file. Then go ahead
and write your binary data as normal.

When you're reading the file, first of all read back the string:

char fileid[7];
int i;
for (i=0;i<7;i++) fileid[i]=0; /* makes sure the string will be 0-terminated */
fread(fileid,6,1,fp);

Then try to match this string to something you recognise (using strcmp
perhaps) and read in the rest of the data.

If you make the ID length 4, you could cast the resultant string pointer
to an integer (Yuck!) and do a switch on it. if (!strcmp(...)) is probably
a better solution, though.

-- 
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019