Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Can't read entire record - fscanf (choked up)

Reply
Thread Tools

Can't read entire record - fscanf (choked up)

 
 
Blankdraw
Guest
Posts: n/a
 
      08-25-2003
....
somewhere, a newbie is dying ...
Is there anybody out there who can help me get the right input
for the following segment? I am trying to read entire records
of 5 (2-digit) integers at a time. It would be best to read the
integers into their own 5 respective variables.
I thought I had it. I've redone my program so many different ways
I am coming to the same conclusion I did when I tried to do it
several years ago: that it cannot reasonably be coded in any popular
language. I just now gave up on reading each individual integer
because I was getting output that looked like my
fscanf(fileptr, "%i", &n); statement was reading entire records instead.
My first record is: 17 40 41 42 46
The following code seems to go only once thru the first 2 FOR loops
(on step-through) and bombs about 46 times thru on the 3rd FOR loop,
with a:
"unhandled exception -- access violation" error.
Using MVC/C++ 4.2 -- is this too "old."
Please HELP (WTH IS GOING ON???)

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <math.h>

int main()
{ /* 52 rows x 120 active columns */
int current[53][122] = {0}; /* include extra R & C for testing */
int n1, n2, n3, n4, n5;
int row=0, col=1, cycle=0;
FILE *fileptr;


if ((fileptr = fopen("test_data.txt", "r")) == NULL )
....error checking ...

for (cycle = 0; cycle < 120; cycle++) /* 5 entries x 120 data recs */
{ /* was 1 to 601 cycles */
fscanf(fileptr, "%i", "%i", "%i", "%i", "%i", &n1, &n2, &n3, &n4, &n5);

printf("%i", "%i", "%i", "%i", "%i", n1, n2, n3, n4, n5);
for (row = 0; row < (n1 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n1 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n1 +1); row < (n2 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n2 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n2 + 1); row < (n3 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n3 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n3 + 1); row < (n4 + 1); row++)
{
current[row][col] = current[row][col - 1];
if (n4 == row) {
current[row][col] = current[row][col] + 1;
}
}

for (row = (n4 + 1); row < 52; row++)
{
current[row][col] = current[row][col - 1];
if (n5 == row) {
current[row][col] = current[row][col] + 1;
}
}
col++;
}
 
Reply With Quote
 
 
 
 
Blankdraw
Guest
Posts: n/a
 
      08-27-2003
First of all, I want to get the one thing about my development
progress so far.
( Eric Sosman last wrote: )

> ... perhaps the problem lies not with the language, but with the
> person unable to use the language; it's a poor workman who blames


I already KNOW this, but if I am going to move on to spend my time on
something of use to myself, I am not going to start out by kicking
myself. And teaching oneself a programming language is unlikely, for
one thing, and not very rewarding for an able-bodied adult. By
itself, I can get better income support from taking on a night job
(janitor, road mantenance, etc.) instead of challenging myself to
expand my horizons.
I just hate to admit defeat without at least pulling out a few
"cheats."

Anyway, I this board has a lot of interesting tips and tricks, for the
occasional solution to a given problem. Eric Sosman's comments here
are really dead-on....


> > for (cycle = 0; cycle < 120; cycle++) /* 5 entries x 120 data recs */
> > { /* was 1 to 601 cycles */
> > fscanf(fileptr, "%i", "%i", "%i", "%i", "%i", &n1, &n2, &n3, &n4, &n5);

>
> This is completely wrong. fscanf() takes *one* format string

....
> What you probably wanted was
>
> fscanf(fileptr, "%i%i%i%i%i", &n1, &n2, &n3, &n4, &n5);
>



This is much better, and seems to work except for the occasional
skipover (n5 on the 7th record, for example.)


> ... but even that isn't very good. fscanf() can fail -- it can....



I checked simple datafile and all records are identical formatting. I
think I will poll a local professor about my logic - show him my
inputs, listing and outputs. I think the program control makes
sense. The only improvement I've been able to add from Eric's code
is that the %i%i... convertors work better as explicitly 2-digit
specifiers: %2i%2i...
I had a ten (10) being read as a 1 - but not 20, 30, 40, or 50!!!
You (eric) mentioned this, I think:

>
> Assuming you've successfully read and converted some numbers,
> putting blind faith in them is a poor idea. What if the input
> accidentally contains "4041" instead of the intended "40 41"?

....

Thanks for the help Eric. I really needed something that looked like
progress cuz I was about to opt for the "indefinite time-out."

Any chance I can get a little more help on one more idea?
Is there a function, in ANSI/ISO basic C, that would "normally" be
used to read a numerical data file (with numerous records of identical
composition) record-by-record into a numerical buffer array (of size
appropriate to 1 record of data), WITHOUT using a structure??? Seems
like this would be a common need in programming, but maybe C requires
use of structures or other advanced I/O techniques for this.


Off-subject item....
I suspect this beginner stuff has been asked by someonbe else in the
past year or so. I really wish google had a special search engine for
newsgroup postings. Technical subjects would benefit from this,
informal ones (alt.binaries.misc.perversion) couldn't, it seems.
Google - you listenin??? You need a form for posters to fill out
with a half-dozen interactive fields to construct a header, above &
before the nearly useless "Thread Subject" title. Interactive like
eBay's listings are offered to sellers. Maybe a little more
ownership in this medium would elevate it above the aimless
exhibitionism it is. I could settle most of my novice problems
through other's mistakes faster than I can pester experts enough that
they will break down and respond directly to pleas for help on a
specific matter.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-27-2003
Blankdraw wrote:
>
> [...] And teaching oneself a programming language is unlikely, for
> one thing, and not very rewarding for an able-bodied adult. [...]


"Unlikely?" Why? My own personal experience is exactly
the opposite: I've learned fifteen-plus programming languages
almost entirely through self-study. I've taken classes in
programming languages only twice, and neither was as successful
a means of learning as was studying the language on my own.

Different strokes for different folks, I guess. Anyhow ...

> Any chance I can get a little more help on one more idea?
> Is there a function, in ANSI/ISO basic C, that would "normally" be
> used to read a numerical data file (with numerous records of identical
> composition) record-by-record into a numerical buffer array (of size
> appropriate to 1 record of data), WITHOUT using a structure??? Seems
> like this would be a common need in programming, but maybe C requires
> use of structures or other advanced I/O techniques for this.


The approach I'd recommend would be to read lines of text
with fgets(), and then use functions like strtol() to extract
the numbers. The reason for doing it this way is that if
something goes wrong -- "1O" instead of "10" in the input,
for example -- you can do a much better job of detecting and
repairing or reporting the problem than you can with fscanf().
Or again: if a line somewhere in the middle of the file has
six numbers or four numbers instead of five, fscanf() won't
consider it an error but will simply get "out of step" with
the line breaks; line-at-a-time-and-pick-it-apart lets you
catch this problem as soon as it crops up.

I'm not sure why you bring up structures in this context.
You seem to want to process your numbers in batches of five,
and there doesn't seem to be a lot of "difference in kind"
between the numbers in each batch. The natural aggregate for
this would appear to be an array of five values, not a struct.

--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning .Net : Select entire month or entire week using selectionmode in calendar control with C# Examples and VB.Net Examples jayeshsorathia@gmail.com ASP .Net 0 09-26-2012 07:17 AM
using fscanf to read data for a class member ... blacksoil@gmail.com C++ 7 09-05-2006 08:54 PM
fscanf to read lines from file? CJ C Programming 2 05-21-2004 11:51 AM
Re: How to put comments in initialization file read by fscanf()? David Rubin C Programming 0 07-14-2003 09:14 PM
Re: How to put comments in initialization file read by fscanf()? Chris Torek C Programming 0 07-14-2003 05:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57