R
Rajen
When I run this program.
#include<stdio.h>
int main()
{
FILE *fp;
int i;
float f;
char str[20];
if( ( fp=fopen("stuff", "w")) == NULL )
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}
for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);
for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);
printf("Hi %s %d %f\n", str, i, f);
}
fclose(fp);
return 0;
}
This is the output:
$./a.out
Hi |àÇù¿-F¦Yâ 0 0.000000
Hi |àÇù¿-F¦Yâ 1 0.000000
Hi |àÇù¿-F¦Yâ 2 0.000000
Hi |àÇù¿-F¦Yâ 3 0.000000
Hi |àÇù¿-F¦Yâ 4 0.000000
Hi |àÇù¿-F¦Yâ 5 0.000000
The contents of the file are:
$cat stuff
0:Hello:12.30
1:Hello:12.30
2:Hello:12.30
3:Hello:12.30
4:Hello:12.30
5:Hello:12.30
6:Hello:12.30
7:Hello:12.30
8:Hello:12.30
9:Hello:12.30
10:Hello:12.30
Why is this happening when I'm reading string and float values. Help me
where I'm wrong
Thanks in advance
#include<stdio.h>
int main()
{
FILE *fp;
int i;
float f;
char str[20];
if( ( fp=fopen("stuff", "w")) == NULL )
{
printf("Sorry! Unable to create/read stuff");
exit(-1);
}
for( i=0; i<=10; i++ )
fprintf(fp, "%d:%s:%.2f\n", i, "Hello", 12.3f);
for( i=0; i<=5; i++)
{
fscanf( fp, "%d:%s:%.2f\n" , &i, &str, &f);
printf("Hi %s %d %f\n", str, i, f);
}
fclose(fp);
return 0;
}
This is the output:
$./a.out
Hi |àÇù¿-F¦Yâ 0 0.000000
Hi |àÇù¿-F¦Yâ 1 0.000000
Hi |àÇù¿-F¦Yâ 2 0.000000
Hi |àÇù¿-F¦Yâ 3 0.000000
Hi |àÇù¿-F¦Yâ 4 0.000000
Hi |àÇù¿-F¦Yâ 5 0.000000
The contents of the file are:
$cat stuff
0:Hello:12.30
1:Hello:12.30
2:Hello:12.30
3:Hello:12.30
4:Hello:12.30
5:Hello:12.30
6:Hello:12.30
7:Hello:12.30
8:Hello:12.30
9:Hello:12.30
10:Hello:12.30
Why is this happening when I'm reading string and float values. Help me
where I'm wrong
Thanks in advance