Implicit loop and reading in an unknown number of variables

I

imailz

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!
 
I

imailz

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?
 
W

Walter Roberson

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

imailz

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

Walter Roberson

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.
[/QUOTE]
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top