Implicit loop and reading in an unknown number of variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • imailz@freenet.de

    Implicit loop and reading in an unknown number of variables

    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,30 0,file);
    sscanf(buffer," %f %f %f\n",&var1,&va r2,&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!
  • Malcolm McLean

    #2
    Re: Implicit loop and reading in an unknown number of variables


    <imailz@freenet .dewrote in message
    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,30 0,file);
    sscanf(buffer," %f %f %f\n",&var1,&va r2,&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!
    do
    {
    fgets(buffer,30 0,file);
    Nvals = sscanf(buffer," %f %f %f\n",&var1,&va r2,&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.

    --
    Free games and programming goodies.


    Comment

    • imailz@freenet.de

      #3
      Re: Implicit loop and reading in an unknown number of variables

      >
      do
      {
      fgets(buffer,30 0,file);
      Nvals = sscanf(buffer," %f %f %f\n",&var1,&va r2,&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?

      Comment

      • Walter Roberson

        #4
        Re: Implicit loop and reading in an unknown number of variables

        In article <2297983c-e570-490e-aa2d-d523d7df94cb@2g 2000hsn.googleg roups.com>,
        <imailz@freenet .dewrote:
        >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

        Comment

        • imailz@freenet.de

          #5
          Re: Implicit loop and reading in an unknown number of variables

          >
          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.

          Comment

          • Walter Roberson

            #6
            Re: Implicit loop and reading in an unknown number of variables

            In article <8e1c3941-049a-49ff-9273-5b1d76871474@x3 5g2000hsb.googl egroups.com>,
            <imailz@freenet .dewrote:
            >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[I][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

            Comment

            Working...