Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Implicit loop and reading in an unknown number of variables

Reply
Thread Tools

Implicit loop and reading in an unknown number of variables

 
 
imailz@freenet.de
Guest
Posts: n/a
 
      06-03-2008
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!
 
Reply With Quote
 
 
 
 
imailz@freenet.de
Guest
Posts: n/a
 
      06-03-2008
>
> 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?

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-03-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
imailz@freenet.de
Guest
Posts: n/a
 
      06-03-2008
>
> 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.
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      06-03-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> 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[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
 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Assign an unknown value to an unknown variable Vincent Arnoux Ruby 1 08-11-2006 06:12 PM
reading from "unknown" number and names of files using fstream jccorreu@gmail.com C++ 7 05-02-2006 03:50 PM
reading the number of unknown rows and columns k_baum217@hotmail.com C Programming 1 03-07-2006 11:53 PM
Processing an unknown number of POST variables to a DB sarah ASP General 1 07-05-2003 06:22 PM



Advertisments