[comp.binaries.ibm.pc.d] UTILITY TO PUT FILENAME AT TOP OF FILE

farris@marlin.NOSC.MIL (Russell H. Farris) (06/27/88)

Can anyone direct me to a utility that reads the name of an MSDOS
file and then inserts the name at the top of the file?  Thanks,

Russ Farris             (farris@marlin.nosc.mil)

wnp@dcs.UUCP (Wolf N. Paul) (06/28/88)

In article <1045@marlin.NOSC.MIL> farris@marlin.NOSC.MIL (Russell H. Farris) writes:
>
>Can anyone direct me to a utility that reads the name of an MSDOS
>file and then inserts the name at the top of the file?  Thanks,
>
>Russ Farris             (farris@marlin.nosc.mil)

I'm not sure that this can be done EXCEPT BY COPYING the entire file. That
method is trivial:

=====================
/* As shown here, works only for files in the current directory.
   To generalize, a function "dirname" would be needed to extract
   the directory name from each filename, as rename() in MSDOS
   ususally does not handle renames across directories very gracefully.
*/
#include <stdio.h>

main(ac, av)
int ac;
char *av[];
{
	FILE *ifp, *ofp, *fopen();
	static char *template="myXXXXXX";
	char *tmpname;
	char c;
	int i=1;

	while ( i != ac )
	{
		tmpname=mktemp(template);	/* I think MSC has this */
		ifp=fopen(av[i],"rb");		/* optionally add error checking here */
		ofp=fopen(tmpname, "wb");	/* optionally add error checking here */
		fprintf(ofp, "%s", av[i]);	/* optionally "%s\n" or whatever */
		while ( (c=getc(ifp)) != EOF )
			putc(c, ofp);			/* this could be done more efficiently */
		fclose(ifp);
		fclose(ofp);
		unlink(av[i]);				/* use whatever you have to delete */
		rename(tmpname, av[i]);		/* and rename or move files */
	}
}
===================


Hope this helps.

Wolf Paul
-- 
Wolf N. Paul * 3387 Sam Rayburn Run * Carrollton TX 75007 * (214) 306-9101
UUCP:     killer!dcs!wnp                 ESL: 62832882
DOMAIN:   wnp@dcs.UUCP                   TLX: 910-380-0585 EES PLANO UD