
- Forum
- Programming Talk
- C and C++
- file handling
file handling
This is a discussion on file handling within the C and C++ forums, part of the Programming Talk category; For the simple file handling program written below to read and print employee details, when i enter details for more ...
-
04-02-2011, 12:38 AM #1
- Join Date
- Apr 2011
- Answers
- 1
file handling
For the simple file handling program written below to read and print employee details,
when i enter details for more than 1 employee, after running the program, the details of the last employee only gets repeatedly printed. May I know y?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long unsigned int pin;
char name[15], empcode[5], city[20];
float bpay,npay,gpay,ta,da,pf,tax;
FILE *fp;
clrscr();
fp=fopen("employee.txt","w+");
printf("\nEnter no. of employees :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the details of the employee %d\n",i+1);
printf("\nEnter Name : ");
fscanf(stdin,"%s",name);
fprintf(fp,"%s\t",name);
printf("\nEnter Employee Code: ");
fscanf(stdin,"%s",empcode);
fprintf(fp,"%s\t",empcode);
printf("\nEnter city : ");
fscanf(stdin,"%s",city);
fprintf(fp,"%s\t",city);
printf("Enter pincode : ");
fscanf(stdin,"%lu",&pin);
fprintf(fp,"%lu\t",pin);
printf("\nBasic Salary : ");
fscanf(stdin,"%f",&bpay);
fprintf(fp,"%f\n",bpay);
}
fclose(fp);
fp=fopen("employee.txt","r+");
printf("\nEmployee Details:\n");
printf("\n\nName\tEmp.ID\tCity\tPin\tBasicPay\tGrossPay\tNetPay\n");
for(i=0;i<n;i++)
{
fscanf(fp,"%s\t%s\t%s\t%lu\t%f\n",name,empcode,city,pin,bpay);
da=bpay*0.05;
pf=bpay*0.1;
ta=bpay*0.03;
tax=bpay*0.01;
gpay+=bpay+ta+da;
npay=gpay-(tax+pf);
fprintf(stdout,"\n%s\t%s\t%s\t%lu\t%.2f\t\t%.2f\t\t%.2f\n",name,empcode,city,pin,bpay,gpay,npay);
da=pf=ta=tax=gpay=npay=0;
}
fclose(fp);
getch();
}
-
04-06-2011, 06:14 AM #2
try read write flag in fopen() and use fcntl.h for file handling...

Reply With Quote





