#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp,*ft;
char another,choice;
struct emp;
{
char name[40];
int age;
float bs;
};
struct emp e;
char empname[40];
long int recsize;

fp=fopen("EMP.DAT","rb+");
if(fp==NULL)
{
fp=fopen("EMP.DAT","wb+");

if(fp==NULL)
{
puts("cannot open file");
exit(1);
}
}
recsize=sizeof(&e);

while(1)
{
clrscr();
gotoxy(30,10);
printf("1.Add records");
gotoxy(30,12);
printf("2.List rcords");
gotoxy(30,12);
printf("0.Exit");
gotoxy(30,20);
printf("Your choice");

fflush(stdin);
choice=getche();
switch(choice)
{
case 1:
fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y')
{
 printf("\nEnter name,age and basic salary");
 scanf("%s%d%f",e.name,&e.age,&e.bs);
 fwrite(&e,recsize,1,fp);
 printf("\nAdd another record(Y/N)");
 fflush(stdin);
 another=getche();
 }
 break;
 case 2:
 rewind(fp);
 while(fread(&e,recsize,1,fp)==1)
 printf("\n%s%d%f",e.name,e.age,e.bs);
 break;
 case '0':
 fclose(fp);
 exit(0);
 }
 }
 }

Please it's not compiling showing some errors help me to execute. And also try to explain me line to line what will happen in this program.

And my final doubt is, in this program i about to create a new record. I want to allocate a code (automatically) for every member who had newly added a record with that they will be able to modify their record. Is it possible if yes, explain me with the code.

Recommended Answers

All 5 Replies

In your previous thread I told you to use code tags, and I provided you a link to the forum announcement, now you're still not able to use them correctly.

Do you also remember what I said about void main() and conio.h ?

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp,*ft;
char another,choice;
struct emp;
{
char name[40];
int age;
float bs;
};
struct emp e;
char empname[40];
long int recsize;

fp=fopen("EMP.DAT","rb+");
if(fp==NULL)
{
fp=fopen("EMP.DAT","wb+");

if(fp==NULL)
{
puts("cannot open file");
exit(1);
}
}
recsize=sizeof(&e);

while(1)
{
clrscr();
gotoxy(30,10);
printf("1.Add records");
gotoxy(30,12);
printf("2.List rcords");
gotoxy(30,12);
printf("0.Exit");
gotoxy(30,20);
printf("Your choice");

fflush(stdin);
choice=getche();
switch(choice)
{
case 1:
fseek(fp,0,SEEK_END);
another='Y';
while(another=='Y')
{
printf("\nEnter name,age and basic salary");
scanf("%s%d%f",e.name,&e.age,&e.bs);
fwrite(&e,recsize,1,fp);
printf("\nAdd another record(Y/N)");
fflush(stdin);
another=getche();
}
break;
case 2:
rewind(fp);
while(fread(&e,recsize,1,fp)==1)
printf("\n%s%d%f",e.name,e.age,e.bs);
break;
case '0':
fclose(fp);
exit(0);
}
}
}

clear my doubt

Please it's not compiling showing some errors help me to execute.

Well, I need to see the error messages, could you post them all down ?

And also try to explain me line to line what will happen in this program.

Did you actually write this code yourself?

Proper indent!

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void main()
{
      FILE *fp,*ft;
      char another,choice;

struct emp;
{
      char name[40];
      int age;
      float bs;
};

      struct emp e;
      char empname[40];
      long int recsize;

      fp=fopen("EMP.DAT","rb+");
      
      if(fp==NULL)
      {
            fp=fopen("EMP.DAT","wb+");

            if(fp==NULL)
            {
                  puts("cannot open file");
                  exit(1);
             }
      }

      recsize=sizeof(&e);

      while(1)
      {
            clrscr();
            gotoxy(30,10);
            printf("1.Add records");
            gotoxy(30,12);
            printf("2.List rcords");
            gotoxy(30,12);
            printf("0.Exit");
            gotoxy(30,20);
            printf("Your choice");
      
            fflush(stdin);
            
            choice=getche();
      
            switch(choice)
            {
                  case 1:
                              fseek(fp,0,SEEK_END);
                              another='Y';
                              while(another=='Y')
                              {
                                    printf("\nEnter name,age and basic salary");
                                    scanf("%s%d%f",e.name,&e.age,&e.bs);
                                    fwrite(&e,recsize,1,fp);
                                    printf("\nAdd another record(Y/N)");
                                    fflush(stdin);
                                    another=getche();
                              }
                              break;
            
            case 2:
                        rewind(fp);
                        while(fread(&e,recsize,1,fp)==1)
                        printf("\n%s%d%f",e.name,e.age,e.bs);
                        break;
            
            case '0':
                              fclose(fp);
                              exit(0);
                  }
      }
}

Remove ; from strcut :)

struct emp
{
      char name[40];
      int age;
      float bs;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.