Mail Archives: djgpp/2000/05/06/21:09:35

From: Damian Yerrick <Bullcr_pd_yerrick AT hotmail DOT comRemoveBullcr_p>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: reading text files
Organization: Pin Eight Software
Message-ID: <>
References: <s0ptgsk9052i4lft8hm38o3r0ugjncb64e AT 4ax DOT com> <Pine DOT SUN DOT 3 DOT 91 DOT 1000503115802 DOT 4851L-100000 AT is> <l476hs8q4tskk01uq232gj9f30scupjea5 AT 4ax DOT com> <200005062141 DOT RAA07172 AT indy DOT delorie DOT com>
X-Newsreader: Forte Agent 1.7/32.534
MIME-Version: 1.0
Lines: 72
X-Trace: +Sdx8bypSHSxdp51phJKWB7xqolzFZ0UkQ7vNPf5Q3LkeRI+CHgYWYetpNxPblqVCzhtokOqG64V!aJln0lS5XuryEis7MlYmL1k4/kZ4yqIzQF4v3h1UMEjg8rEe4ptEIvT7x6G1J1RqvkdpxfdBBtYa!OTB7VhA=
X-Complaints-To: abuse AT gte DOT net
X-Abuse-Info: Please be sure to forward a copy of ALL headers
X-Abuse-Info: Otherwise we will be unable to process your complaint properly
NNTP-Posting-Date: Sun, 07 May 2000 00:21:00 GMT
Distribution: world
Date: Sun, 07 May 2000 00:21:00 GMT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Sat, 6 May 2000 17:41:43 -0400 (EDT), Eli Zaretskii
<eliz AT delorie DOT com> wrote:

>Damian Yerrick wrote:
>> int main(void)
>> {
>>   char foo[16];
>>   FILE *fp = fopen("foo.txt", "rb");
>>   if(!fp)
>>   {
>>     puts("couldn't open foo.txt for writing.\n"
>>          "It should contain one line with one very long word.");
>>     return 1;
>>   }
>>   fscanf(fp, "%s", foo);
>>   printf("read word: %s\n", foo);
>>   return 0;
>If you replace fscanf with fgets+sscanf in this case, without changing
>the format or anything else, it will blow up the stack in exactly the
>same way.  Did you try it?

But if you tell fgets() to read only n characters, where n is small
enough to fit comfortably into your buffer, everything will be nice.

>> sscanf() knows that no incoming string will be longer than the input
>> string.

What was I drinking when I wrote this?

>Sorry, I cannot parse this statement.  Care to explain?

char foo[16];
char bar[16];

fgets(foo, 16, fp);
sscanf(foo, "%s", bar);

This will never return more than 15 (+1) characters in bar
because foo can never have more than 15 (+1) characters.

>Also, the reason for the crash in the program you posted is that
>the buffer foo[] is too small to accept the input from the file.

Exactly my point. Using fscanf() can result in buffer overflows
because there is no maximum size for the output buffers.

>`sscanf' cannot solve this problem, since it doesn't know how large
>is its third argument.  It only knows how large is its first argument.

Yes, but the other arguments can be made large enough to hold the
largest thing that sscanf() can put into them; they're never larger
than the first argument, and if the first argument has a known maximum
size (thanks to fgets()), sscanf() cannot overflow the buffer.

>Finally, there should be no reason to use `fscanf' to read a string
>with "%s" format.  `fscanf' is for converting text into non-text data,
>and when used as such, `sscanf' and `fscanf' behave even closer
>(i.e. blow or not in the same way).

It was an illustrative example to show that fscanf() suffers from the
same disease as gets().

Damian Yerrick
"I refuse to listen to those who refuse to listen to reason."
See the whole sig:

This is McAfee VirusScan. Add these two lines to your signature to
prevent the spread of signature viruses.

- Raw text -

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