ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/04/25/00:47:44

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "Goh, Yong Kwang" <gohyongkwang AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: detecting EOF when using fscanf()
Date: Thu, 25 Apr 2002 12:38:50 +0800
Organization: Singapore Telecommunications Ltd
Lines: 99
Message-ID: <aa819p$tge$1@coco.singnet.com.sg>
NNTP-Posting-Host: tns03821.singnet.com.sg
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I'm currently using scanf in a while loop to read in a text data file which
has a format slightly similar to a makefile, a text script-like file
separated by \n and \t.

Example:
---START---
Component cSearchBox "search.html"\n
Component cNavBar "navbar.html"\n
Component cPageCounter "counter.html"\n

Template tMain "main.html"\n
\t cSearchBox\n
\t cNavBar\n
\t cPageCounter\n

Document dMain "doc1.html"\n
\t tMain\n
---END---
The \n and \t are placeholder for illustration purpose only.
Since each token is a string, so i always call fscanf(FH, "%s", &buffer); to
read in each token one-by-one until there's no more left.

The problem occurs after reading in the last token "tMain" as the following
sample output indicates:
---START---
D:\TEMP>readfile
Component
cSearchBox
"search.html"
Component
cNavBar
"navbar.html"
Component
cPageCounter
"counter.html"
Template
tMain
"main.html"
cSearchBox
cNavBar
cPageCounter
Document
dMain
"doc1.html"
tMain
tMain

D:\TEMP>
---END---

Notice the last token "tMain" got printed twice which infers that the loop
was repeated another 1 last time after the last token.
---START---
#include <stdio.h>

main(){
char buffer[80];
FILE* CONFIG;
CONFIG = fopen("config.txt", "rt");
if(CONFIG != NULL){
        while(!feof(CONFIG)){
                fscanf(CONFIG, "%s", &buffer);
                printf("%s\n", buffer);
        }
        fclose(CONFIG);
}
else{
        perror("Cannot open file!\n");
}
return 0;
}
---END---
After reading in the last token, the loop doesn't terminate immediately,
which I suspect is due to the \n character after the "tMain". Since the EOF
character is not immediately present after "tMain", the loop goes through
another time but this time it fails to find any more string token,
encounters the EOF char and then terminates.

I cannot check for such a condition by comparing if the newly-read token is
the same token as the one read in previous loop, strcmp(old, new), since
there may be a legitimate line with the same 2 tokens *actually* appearing
in consecutive order e.g. Component Component.

So is there any easy way to overcome this problem, or do I have to do the
tokenizing and parsing character by character using fgetc instead for the
program to work more reliably?

--
Yong-Kwang Goh
Singapore
gohyongkwang AT hotmail DOT com


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.351 / Virus Database: 197 - Release Date: 4/19/02


- Raw text -


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