D
dustu
Hi,
I'm using lcc-win32 on Windows 2000 as compiler/IDE. The details are:
Input file (sample - the no. of digits per line may change):
--->
1111111111111111.00
1111111111111111.01
1111111111111111.02
<---
Objective : Reading each line and displaying the sum at the end
Desired output : 3333333333333333.03
Current output : 3333333333333333.000000
Code :
--->
#include<stdio.h>
#include<stdlib.h>
int read_lines(char *filename)
{
long double sum ;
char buf[256] ;
int new_line_ascii = 10 ;
char i ;
FILE *ffile ;
if ((ffile = fopen(filename,"r"))==NULL) { return 2 ; }
sum=0 ;
while (fgets(buf,sizeof(buf), ffile)) { sum+=atof(buf) ; }
printf ("%LF\n",sum) ;
return fclose(ffile) ;
}
int main(int argc, char *argv[])
{
char *fname ;
int err_code, j = 0 ;
if (argc < 2) {
err_code = 2 ;
printf("Usage : %s <filename> \n",argv[0]) ;
}
else {
for (j=1;j<argc;j++) {
err_code = read_lines((fname = argv[j])) ;
if (err_code != 0 ) {
printf("\nError: Could not open file [%s]\n",fname) ;
}
}
}
return err_code ;
}
<---
I'm using lcc-win32 on Windows 2000 as compiler/IDE. The details are:
Input file (sample - the no. of digits per line may change):
--->
1111111111111111.00
1111111111111111.01
1111111111111111.02
<---
Objective : Reading each line and displaying the sum at the end
Desired output : 3333333333333333.03
Current output : 3333333333333333.000000
Code :
--->
#include<stdio.h>
#include<stdlib.h>
int read_lines(char *filename)
{
long double sum ;
char buf[256] ;
int new_line_ascii = 10 ;
char i ;
FILE *ffile ;
if ((ffile = fopen(filename,"r"))==NULL) { return 2 ; }
sum=0 ;
while (fgets(buf,sizeof(buf), ffile)) { sum+=atof(buf) ; }
printf ("%LF\n",sum) ;
return fclose(ffile) ;
}
int main(int argc, char *argv[])
{
char *fname ;
int err_code, j = 0 ;
if (argc < 2) {
err_code = 2 ;
printf("Usage : %s <filename> \n",argv[0]) ;
}
else {
for (j=1;j<argc;j++) {
err_code = read_lines((fname = argv[j])) ;
if (err_code != 0 ) {
printf("\nError: Could not open file [%s]\n",fname) ;
}
}
}
return err_code ;
}
<---