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

Discussion in 'C Programming' started by Blankdraw, Aug 25, 2003.

  1. Blankdraw

    Blankdraw Guest

    ....
    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++;
    }
    Blankdraw, Aug 25, 2003
    #1
    1. Advertising

  2. Blankdraw

    Blankdraw Guest

    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.
    Blankdraw, Aug 27, 2003
    #2
    1. Advertising

  3. Blankdraw

    Eric Sosman Guest

    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.

    --
    Eric Sosman, Aug 27, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Malcolm Dew-Jones

    My XML Feed Choked

    Malcolm Dew-Jones, Nov 3, 2005, in forum: XML
    Replies:
    3
    Views:
    4,138
    Joe Cox
    Nov 4, 2005
  2. Chris Torek
    Replies:
    0
    Views:
    381
    Chris Torek
    Jul 14, 2003
  3. David Rubin
    Replies:
    0
    Views:
    428
    David Rubin
    Jul 14, 2003
  4. CJ

    fscanf to read lines from file?

    CJ, May 20, 2004, in forum: C Programming
    Replies:
    2
    Views:
    2,977
    Dan Pop
    May 21, 2004
  5. Richard Muller

    IM died - here's the msg it choked on

    Richard Muller, Oct 16, 2003, in forum: ASP General
    Replies:
    1
    Views:
    123
    Aaron Bertrand - MVP
    Oct 16, 2003
Loading...

Share This Page