Ganesh program works good.But,I want to add one exception handling in it.
In his program,he opened the file in append mode.If the file not exits,then it will create and it will seek for string from the file.Empty string will be returned.sscanf function will seek for float values in the string.There is no string at all.Then,it will give some garbage value.To avoid this,you change ganesh code into the following.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
float x,y,z;
FILE *fp=fopen("temp", "r+");
if(fp==NULL)
{
printf ( "No File Found.Create It and Run the Program.\n" );
perror("File");
exit(1);
}
char str[20], buf[20];
printf("Enter 3 numbers:");
fgets(str, 20, fp);
printf ( "STRING:%s\n",str );
sscanf(str,"%f,%f,%f", &x,&y,&z); // string to float conversion
printf("%f %f %f\n",x,y,z);
sprintf(buf,"%f,%f,%f",x,y,z); // float to string conversion
printf("buf:%s\n",buf);
return 0;
}