複数のファイルにあるデータをもとに文を作るプログラム

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*定数の定義*/
#define WORDMAX 500	/*単語の最大数*/
#define BUFLEN 256	/*1行の最大バイト数*/

void getWord(char *fileName, char *buf)
{
	char tmpStr[BUFLEN];
	int count = 0, selected;
	long wordLocation[WORDMAX];
	FILE *stream;
	
	if((stream = fopen(fileName, "r")) == NULL){
		printf("ファイル%sが開けません。\n", fileName);
	exit(1);
	}
	
	/*ファイル中の単語の位置を記録する*/
	while(count < WORDMAX){
		wordLocation[count] = ftell(stream);
		if(fgets(tmpStr, BUFLEN, stream) == NULL)
			break;
		count++;
	}
	
	/*単語の中から一つ選ぶ*/
	selected = rand() % count;
	fseek(stream, wordLocation[selected], SEEK_SET);
	fgets(buf, BUFLEN, stream);
	fclose(stream);
	
	/*改行文字を削除する*/
	if(*(buf + strlen(buf) - 1) == '\n')
		*(buf + strlen(buf) - 1) = '\0';
}

int main(int argc, char *argv[])
{
	char lineBuf[BUFLEN], wordBuf[BUFLEN], *p, *percent;
	int kakomare;	/* 1 = %で囲まれている */
	FILE *inStream;
	
	if(argc != 2){
		printf("sakubun 入力ファイル名\n");
		exit(0);
	}
	
	if((inStream = fopen(argv[1], "r")) == NULL){
		printf("ファイル%sがオープンできません。\n", argv[1]);
		exit(1);
	}
	
	srand(time(NULL) % 100);
	
	/*行単位の処理をするループ*/
	while(fgets(lineBuf, BUFLEN, inStream) != NULL){
		p = lineBuf;
		kakomare = 0;
		
		/*行内の処理をするループ*/
		while(1){
			/*%を探す*/
			percent = strchr(p, '%');
			if(percent != NULL)
				*percent = '\0';
			
			/* %で区切られた部分を処理する*/
			if(kakomare){
				getWord(p, wordBuf);
				printf("%s", wordBuf);
			} else{
				printf("%s", p);
			}

			/*行末までいったらループを抜ける*/
			if(percent == NULL) break;
			
			/* pを%の次に移動してループ先頭に戻る*/
			p = percent + 1;
			kakomare = 1 - kakomare;
		}
	}
	fclose(inStream);
	
	return 0;
}
sakubun.txt
吾輩は%animal.txt%である。
名前は%name.txt%。
生まれたところは%where.txt%。
%favor.txt%と%favor.txt%が好き。
実行結果
D:\cp>6-5 sakubun.txt
吾輩はライオンである。
名前は一郎。
生まれたところは家の中。
アイスクリームとカレーライスが好き。