🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2368

Re: fscanf problem

#2368
From: John Touhig
Date: Sun, 24 Mar 1996 17:40
70 lines
3079 bytes
Michael Smith <msmith@mpx.com.au> wrote:

>sg7248613@omega.ntu.ac.sg wrote:
>> 
>> I have been trying to read a section of data from a file for days.
>> 
>> The data segment goes like this:
>>     .....
>>     Patch norm0 4 { { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } }
>>     Patch vert0 4 { { 0 1 0 } { 1 1 0 } { 1 1 1 } { 0 1 1 } }.
>>     .....
>> My actual code segment is as follows:
>> 
>> fscanf(meshf, "    Patch %s %d { { %f %f %f } { %f %f %f } { %f %f %f }\
>>             { %f %f %f } }\n    Patch %s %d { { %f %f %f } { %f %f %f }\
>>             { %f %f %f } { %f %f %f } }\n", pptr->nname, &(pptr->numVert),
>>             &tmpf[0], &tmpf[1], &tmpf[2], &tmpf[3], &tmpf[4], &tmpf[5],
>>             &tmpf[6], &tmpf[7], &tmpf[8], &tmpf[9], &tmpf[10], &tmpf[11],
>>             pptr->vname, &tmpi,
>>             &tmpg[0], &tmpg[1], &tmpg[2], &tmpg[3], &tmpg[4], &tmpg[5],
>>             &tmpg[6], &tmpg[7], &tmpg[8], &tmpg[9], &tmpg[10], &tmpg[11]);
>> 
>> The read of the first line is OK. But I can't scanf the second line
>> successfully. Could anyone please enlighten me on this? Thank you!

>I suspect that the problem is in the trailing '\n' character, but fscanf 
>syntax is always messy.  If you leave out the trailing '\n', the leading 
>spaces in the format string will cause the program to skip over any white 
>space, including newlines.

>***BUT***

>Where your records are newline-delimited I normally find it easier to 
>read in a line at a time with fgets() and then parse it with sscanf()

>e.g.

>{
>	#define RECSIZE 80 /* or whatever will be big enough */
>	char buff[RECSIZE];

>	/* error handling left out for brevity */
>	fgets(buff, RECSIZE, meshf);
>	sscanf( buff, ".....", ..... );

>}

>-- 
>#####################################################################
>Michael Smith                                       msmith@mpx.com.au
>Emmenjay Consulting                 http://www.hutch.com.au/~emmenjay
>#####################################################################

I will totaly agree with Michael, listen to him he is talking sense,
the trouble with the "c" programming language is that people try to
show off buy writing unreadable and unmaintenable code and they think
thier good. Unfoutunatley they are the bug bear of the programming
world. I have taught "C" programming for a number of years and the
problems I've had getting people to write GOOD "C" code is
indiscripable.

*******
I hope you enjoy the world of "C"


John Touhig


jtouhig@netwales.co.uk
*******
Technical Details
Message-ID: <4j41f5$ng6@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4imaao$dsi@solutions.solon.com> <4j06k8$7sn@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: jtouhig@netwales.co.uk (John Touhig)
Newsgroups: comp.lang.c.moderated
Subject: Re: fscanf problem
Date: 24 Mar 1996 11:40:53 -0600
Organization: Syntech Computer Systems
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4j41f5$ng6@solutions.solon.com>
References: <4imaao$dsi@solutions.solon.com> <4j06k8$7sn@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Forte Free Agent 1.0.82