I have the following data file
1.0
2.0
3.0
This is an a file text.dat. The following code I have so far is in mygetline.c and I compile it into the executable mygetline. Executing it I feed the data file into the executable thus
./mygetline < text.dat in the bash terminal.
I want to read in the data file like that and print to stdout that column, plus some function of it. Here is the code so far.
#include <stdio.h>
int mygetline ( char s[], int lim )
{
int c, i;
i = 0;
while( --lim > 0 && ( c = getchar() ) != EOF && c != '\n' )
s[i++] = c;
if ( c == '\n' )
s[i++] = c;
s[i] = '\0';
return i ;
}
void main()
{
const int maxline = 10000;
int nl, length;
char line[maxline];
double value ;
nl = 0;
while ( ( length = mygetline( line, maxline ) ) != 0 ) //avoiding blanks
{
//Original error sscanf( "%lf", line, &value ) ; //trying to get each line
//as the right
//number
//FOllowing line is corrected implementation of sscanf()
sscanf( line, "%lf", &value ) ;
printf( "%lf %lf\n", value ,value*value ) ; //trying to output x & x*x
}
}
the output is the following
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
I want something like
1.0 1.0
2.0 4.0
3.0 9.0
to whatever precision.
Does anyone have any suggestion as to what I am missing to get my desired output? Thank you
Re-read the documentation for
sscanf.The format is the second parameter.
But you put the
"%lf"as the first parameter.