Implicit loop and reading in an unknown number of variables

Discussion in 'C Programming' started by imailz@freenet.de, Jun 3, 2008.

  1. Guest

    Hi all,

    since I'm forced to switch from Fortran to C I wonder if there is
    posibility in C:

    1) to use implicit loops
    2) to parse several variables which number is determined at runtime.

    Following example:
    The output contains n columns which have to be read in. The number of
    the columns is determined only at runtime. The length of the columns
    is m. So an implicit loop can be used to read in the elements. In
    Fortran this problem can be solved by three lines:

    READ(*,*),n
    x='(nF10.5)'
    READ(*,x),((var(i,j),i=1,n),j=1,m)

    To do the same in C I have a temporary solution:

    for (i=0;i<m;i++)
    {
    fgets(buffer,300,file);
    sscanf(buffer,"%f %f %f\n",&var1,&var2,&var3);
    }

    Which is also ok, but the number of variables is already defined at
    compilation. Since this procedure has to be repeated several times, I
    would like to avoid any "if"-constructs. Are there similiar tools in
    C like the two aforementioned in Fortran?

    Thanks in advance!
     
    , Jun 3, 2008
    #1
    1. Advertising

  2. Guest

    >
    > do
    > {
    > fgets(buffer,300,file);
    > Nvals = sscanf(buffer,"%f %f %f\n",&var1,&var2,&var3);
    >
    > } while(Nvals == 3);
    >
    > /* at this point buffer contains the line following the list of 3 floats */
    >
    > If the number of columns as well as the number of rows is unknown it is a
    > bit trickier. If you go onto my website you can find code to read a
    > coma-separated values file.



    Thank you for fast reply! Maybe my description was a little bit too
    short.

    The number of rows and of columns is read from the input. So there are
    two variables for that n and m which are known after the beginning of
    the program. My problem is that I don't know how to make the number of
    the variables flexible or dynamic in "sscanf". In Fortran it's
    controlled it by Format and implicit loop over the columns and rows.
    But what to do in C?
     
    , Jun 3, 2008
    #2
    1. Advertising

  3. In article <>,
    <> wrote:

    >The number of rows and of columns is read from the input. So there are
    >two variables for that n and m which are known after the beginning of
    >the program. My problem is that I don't know how to make the number of
    >the variables flexible or dynamic in "sscanf". In Fortran it's
    >controlled it by Format and implicit loop over the columns and rows.
    >But what to do in C?


    No need, just fscanf() with a single "%f" format inside loops
    that handle one element at a time and store the received value into
    an appropriate location.

    --
    "Ignorance has been our king... he sits unchallenged on the throne of
    Man. His dynasty is age-old. His right to rule is now considered
    legitimate. Past sages have affirmed it. They did nothing to unseat
    him." -- Walter M Miller, Jr
     
    Walter Roberson, Jun 3, 2008
    #3
  4. Guest

    >
    > No need, just fscanf() with a single "%f" format inside loops
    > that handle one element at a time and store the received value into
    > an appropriate location.
    >

    OK, the number of formats is solved, but how can I handle the number
    of variables (=columns)? There have to be given explicitly.
     
    , Jun 3, 2008
    #4
  5. In article <>,
    <> wrote:

    >> No need, just fscanf() with a single "%f" format inside loops
    >> that handle one element at a time and store the received value into
    >> an appropriate location.


    >OK, the number of formats is solved, but how can I handle the number
    >of variables (=columns)? There have to be given explicitly.


    In C, all of the format elements except %c and %[ skip leading whitespace
    in the input before they attempt to read. newline indicators are a form
    of whitespace (along with spaces, tabs, and vertical tabs.) So if your
    data happens to consist of 3 columns and you do four reads with %f
    format, the first three reads will pull the data off of the first line,
    then the fourth read would see the end of line as just another kind
    of whitespace and would skip over it and position to start reading the
    first number on the second line.

    Thus, in C, if you are reading numbers with a consistant format and the
    absence of a number is not significant, then just keep reading with the
    same format and fscanf will pull out all of the values in sequence.
    No need to tell fscanf how many numbers there are per line.

    In outline,

    for (I = 1; I <= N; I++ )
    for (J = 1; J <= M; J++ )
    fscanf("%f", &datamatrix[J]);

    But you would want to enhance this with some error checking so you
    abort cleanly on (unexpected) end of file or upon a data error (e.g.,
    a stray text character slipped into the file.)
    --
    "What is important, then, is not that the critic should possess a
    correct abstract definition of beauty for the intellect, but a
    certain kind of temperament, the power of being deeply moved by
    the presence of beautiful objects." -- Walter Pater
     
    Walter Roberson, Jun 3, 2008
    #5
    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. Replies:
    1
    Views:
    348
    A. Sinan Unur
    Mar 7, 2006
  2. Replies:
    7
    Views:
    691
    Lyell Haynes
    May 2, 2006
  3. sarah
    Replies:
    1
    Views:
    122
    Chris Barber
    Jul 5, 2003
  4. Vincent Arnoux
    Replies:
    1
    Views:
    272
    Arnaud Bergeron
    Aug 11, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    445
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page