delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/25/10:21:11

From: fredex AT fcshome DOT stoneham DOT ma DOT us
Message-Id: <199605251401.KAA05459@fcshome.stoneham.ma.us>
Subject: djgpp 2.0 fscanf bug??
To: djgpp AT delorie DOT com
Date: Sat, 25 May 1996 10:01:55 -0400 (EDT)

I've just picked up the djgpp 2.0 package from   oak.oakland.edu the
other day, never having used it before. I've run into a problem with a
piece of code that works on several other platforms but which fails
under djgpp (See short program below). It seems to me that fscanf is not
failing (and therefore returning <= 0) when input does not match the
format, which is what I think it SHOULD be doing. In the particular
example, there are a series of lines from which four tokens are read
"%d%s%s%s", followed by a line with only one token. Other fscanf
implementations I've worked with return <= 0 when they hit this line,
but the one in djgpp seems to return 4, with incorrect result
assignments. OTOH, I may be wrong and it may be something more subtle
than that. I'd appreciate any assitance/ guidance you could offer.
(If I were writing this code I'd do it differently, but I'd like
to avoid re-inventing it if possible.)

BTW, I've not yet joined the list, so I'd appreciate it if you could
reply direct to me.

Thanks!

------------------------------- The Sample Program ----------------------------

#include <stdio.h>

main (int argc, char ** argv)
	{
	char segTypes[256], typ[10], ro[10], nr[20];
	int sto = 0, fldLen, n = 0;
	FILE * infile;

	if (argc != 2)
		{
		fprintf (stderr, "args!\n");
		exit (0);
		}

	infile = fopen (argv[1], "r");
	if (infile == (FILE *)0)
		{
		fprintf (stderr, "oops!\n");
		exit (0);
		}

	while ((n = fscanf (infile, "%d%s%s%s", &fldLen, typ, ro, nr)) > 0)
		{
		fgets (segTypes, sizeof (segTypes), infile);
		printf ("n=%d, fldLen=%d, typ=%s, ro=%s, nr=%s, pos=%ld\n",
			n, fldLen, typ, ro, nr, ftell (infile));
		}
	printf ("lastpos=%ld\n", ftell (infile));
	fclose (infile);
	}
------------------------------- The Data File ---------------------------------
1 ST REQ NO_RPT		/* 1  Field Separator           */
4 ST REQ NO_RPT		/* 2  Encoding Characters       */
15 ST OPT NO_RPT 	/* 3  Sending Application       */
20 ST OPT NO_RPT 	/* 4  Sending Facility          */
15 ST OPT NO_RPT 	/* 5  Receiving Application     */
30 ST OPT NO_RPT  	/* 6  Receiving Facility        */
19 TS OPT NO_RPT  	/* 7  Date/Time of Message      */
40 ST OPT NO_RPT  	/* 8  Security                  */
7 ID REQ NO_RPT   	/* 9  Message Type              */
20 ST REQ NO_RPT  	/* 10 Message Control ID        */
1 ID REQ NO_RPT   	/* 11 Processing ID             */
8 NM REQ NO_RPT   	/* 12 Version ID                */
15 NM OPT NO_RPT   	/* 13 Sequence Number           */
180 ST OPT NO_RPT  	/* 14 Continuation Pointer      */
MSH

2 ID REQ NO_RPT   	/* 1  Acknowledgement Code      */
20 ST REQ NO_RPT	/* 2  Message Control ID        */
80 ST OPT NO_RPT  	/* 3  Text Message              */
15 NM OPT NO_RPT   	/* 4  Expected Sequence Number  */
1 ID OPT NO_RPT 	/* 5  Delayed Ack Type          */
MSA

------------------------------ End of sample data -----------------------------

-- 
-------------------------------------------------------------------------------
 .----    Fred Smith    /                        Office: fred AT computrition DOT com 
( /__  ,__.   __   __ /  __   : /                                 508-663-2524 
 /    /  /   /__) /  /  /__) .+'           Home: fredex AT fcshome DOT stoneham DOT ma DOT us 
/    /  (__ (___ (__(_ (___ / :__                                 617-438-5471 
-------------------------------- Jude 1:24,25 ---------------------------------

- Raw text -


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