🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c.moderated
7 messages
7 total messages Started by sg7248613 Tue, 19 Mar 1996 12:46
fscanf problem
#2330
Author: sg7248613
Date: Tue, 19 Mar 1996 12:46
20 lines
1364 bytes
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!
Re: fscanf problem
#2358
Author: Michael Smith
Date: Sat, 23 Mar 1996 06:44
50 lines
2423 bytes
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
#####################################################################
Re: fscanf problem
#2364
Author: Your Name
Date: Sat, 23 Mar 1996 06:48
29 lines
1793 bytes
In article <4imaao$dsi@solutions.solon.com>, sg7248613@omega.ntu.ac.sg 
says...
 >
 >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 think the \n may be causing the problem.  Why not remove \n and it will 
be consumed as whitespace?
Re: fscanf problem
#2368
Author: 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
*******
Re: fscanf problem
#2396
Author: Tanmoy Bhattacharya
Date: Wed, 27 Mar 1996 00:59
48 lines
2688 bytes
In article <4imaao$dsi@solutions.solon.com>
sg7248613@omega.ntu.ac.sg writes:

s: I have been trying to read a section of data from a file for days.
s: 
s: The data segment goes like this:
s:     .....
s:     Patch norm0 4 { { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } }
s:     Patch vert0 4 { { 0 1 0 } { 1 1 0 } { 1 1 1 } { 0 1 1 } }.
                                                                ^
The format string makes no allowance for the last character above.

s:     .....
s: My actual code segment is as follows:
s: 
s: fscanf(meshf, "    Patch %s %d { { %f %f %f } { %f %f %f } { %f %f %f }\
s:             { %f %f %f } }\n    Patch %s %d { { %f %f %f } { %f %f %f }\
s:             { %f %f %f } { %f %f %f } }\n", pptr->nname, &(pptr->numVert),
s:             &tmpf[0], &tmpf[1], &tmpf[2], &tmpf[3], &tmpf[4], &tmpf[5],
s:             &tmpf[6], &tmpf[7], &tmpf[8], &tmpf[9], &tmpf[10], &tmpf[11],
s:             pptr->vname, &tmpi,
s:             &tmpg[0], &tmpg[1], &tmpg[2], &tmpg[3], &tmpg[4], &tmpg[5],
s:             &tmpg[6], &tmpg[7], &tmpg[8], &tmpg[9], &tmpg[10], &tmpg[11]);
s: 
s: The read of the first line is OK. But I can't scanf the second line
s: successfully. Could anyone please enlighten me on this? Thank you!

Please post short complete compilable examples when asking
questions. It is not clear to me what you mean by `I can't scanf the
second line successfully'. What happens? Does the program crash? Does
it print a message saying `I can't scanf the second line
successfully'? 

Which second line? The format would read two lines of the form that
you gave in one scanf. 

(I would have ignored such a vaguely worded post, but the other
followups I see are incorrect, so I had to respond.)

Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Re: fscanf problem
#2432
Author: sg7248613
Date: Fri, 29 Mar 1996 12:08
60 lines
2492 bytes
In article <4ja3tv$pdl@solutions.solon.com>, tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya) writes:
> In article <4imaao$dsi@solutions.solon.com>
> sg7248613@omega.ntu.ac.sg writes:
> 
> s: I have been trying to read a section of data from a file for days.
> s:     .....
> s:     Patch norm0 4 { { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } }
> s:     Patch vert0 4 { { 0 1 0 } { 1 1 0 } { 1 1 1 } { 0 1 1 } }.

***** edited and deleted *****	

> s: The read of the first line is OK. But I can't scanf the second line
> s: successfully. Could anyone please enlighten me on this? Thank you!
> 
> Please post short complete compilable examples when asking
> questions. It is not clear to me what you mean by `I can't scanf the
> second line successfully'. What happens? Does the program crash? Does
> it print a message saying `I can't scanf the second line
> successfully'? 
> 
> Which second line? The format would read two lines of the form that
> you gave in one scanf. 
> 
> (I would have ignored such a vaguely worded post, but the other
> followups I see are incorrect, so I had to respond.)
> 
> Cheers
> Tanmoy

I am sorry that I have not made myself absolutely clear.

What I meant was that the first line of data which goes like

	Patch norm0 4 { { 0 -1 0 } ....

was successfully scanfed into a data structure and printed.

The second line

	Patch vert0 4 { { 0 1 0 } ....

encountered problems like segmentation faults or bus errors.

I have tried many variations of the code segments before seeking
help on this newsgroup. The code segment I posted was a generic
example of what I attempted myself.

I would like to take this opportunity to thank all the people who
have responded to my post, either by email or followup.

You have truly made a difference in the progress of my project.
I have since moved on to code other portions of my system, after
resolving this file-reading portion.

Thank you all again!


Tang Li Chow
Nanyang Technological University
Singapore
Re: fscanf problem
#2543
Author: George Swan
Date: Mon, 08 Apr 1996 16:08
45 lines
2309 bytes
In article <4jgjs2$sao@solutions.solon.com>,
 <sg7248613@gamma.ntu.ac.sg> wrote:
>In article <4ja3tv$pdl@solutions.solon.com>, tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya) writes:
>> In article <4imaao$dsi@solutions.solon.com>
>> sg7248613@omega.ntu.ac.sg writes:
>> 
>> s: I have been trying to read a section of data from a file for days.
>> s:     .....
>> s:     Patch norm0 4 { { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } { 0 -1 0 } }
>> s:     Patch vert0 4 { { 0 1 0 } { 1 1 0 } { 1 1 1 } { 0 1 1 } }.

>***** edited and deleted *****	

>> Please post short complete compilable examples when asking
>> questions. It is not clear to me what you mean by `I can't scanf the
>> second line successfully'. What happens? Does the program crash? Does
>> it print a message saying `I can't scanf the second line
>> successfully'? 

>I am sorry that I have not made myself absolutely clear.

>What I meant was that the first line of data which goes like

>	Patch norm0 4 { { 0 -1 0 } ....

>was successfully scanfed into a data structure and printed.

>The second line

>	Patch vert0 4 { { 0 1 0 } ....

>encountered problems like segmentation faults or bus errors.

I haven't seen anything about the absense of the checking of return
codes in the original example and the various followups.

I'd contend that if the original poster wanted to do a scanf( ) to
read in all 28 data items, then that code should check that scanf()
returned 28.  I've seen many programs were the programmers had
chosen to see whether scanf() returned EOF.

In my opinion this is dead wrong.  The input might contain something
that didn't scan as a number.  The program would be reading the
tokens out of synch.  And some of the data elements would still
contain their values prior to initialization.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads