append mode ("a") query

chet at uwovax.uwo.ca chet at uwovax.uwo.ca
Wed Feb 14 03:04:59 AEST 1990


I know I'll be embarrassed because of this, but would someone please
tell me why program A below, when executed twice contains twenty lines
(i.e. the 2nd ten lines are inserted before the EOF mark), but program B
positions its appends after the EOF mark (so that they don't appear when
the file is typed out)? And what I can do to fix B. This happens on
Turbo C and Small C (2.2), and I presume on every C compiler.

Please reply to creider at csd.uwo.ca

Program A:
#include <stdio.h>
char entry[35];
main()
{
	FILE *fp;
	int i;

	fp = fopen("tenlines.txt","a");
	strcpy(entry,"this is an entry line");
	for (i=0; i<10; i++) 
		fprintf(fp, "%s\n", entry);
	fclose(fp);
}

==========================================
Prog B:

#include <stdio.h>

char entry[280];
char main_entry[40];
char lexical_class[20];
char eng_gloss[40];
char gram_info[80];
char semantic_fields[40];
char subent[80];
	
main()
{
	FILE *fp;

	fp=fopen("lex3.db","a");

	printf("\n\nMain entry: ");
	add_subentry(entry, main_entry);
	printf("Lexical class: ");
	add_subentry(entry, lexical_class);
	printf("English gloss: ");
	add_subentry(entry, eng_gloss);
	printf("Grammatical information: ");
	add_subentry(entry, gram_info);
	printf("Semantic fields: ");
	add_subentry(entry, semantic_fields);

	fprintf(fp, "%s\n", entry); 
	fclose(fp);	
}

add_subentry(entry, subentry)
char *entry, *subentry;
{
	gets(subentry);
	strcat(subentry,"$");
	strcat(entry,subentry);
}


Many thanks,

Chet Creider



More information about the Comp.lang.c mailing list