I have some C code that works well:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp=fopen("EMPLOYEE.DAT","r");
if(fp==NULL)
{
puts("Cannot open file";
exit(1);
}
while(fscanf(f,"%s %d %f",&e.name,&e.age,&e.bs)!=EOF)
printf("%s %d %f\n",e.name,e.age,e.bs);
fclose(fp);
return 0;
}
data inside EMPLOYEE.DAT
:
Sunil 34 1250.50
Sameer 21 1300.50
rahul 34 1400.50
I'm having trouble translating this code to Python:
while(fscanf(f,"%s %d %f",&e.name,&e.age,&e.bs)!=EOF)
printf("%s %d %f\n",e.name,e.age,e.bs);
Is there any way to implement that in Python? Furthermore, what are Pythonic alternatives of exit()
& EOF
?