Hi,
The problems of this code are:
Why switch(argv[1]) do not work?
Each time I write new information to the file the previous ones are deleted.
If you see any improvements that can be added to the code I would be grateful
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct staff{
char name[30];
int age;
};
int main(int argc, char *argv[])
{
struct staff record;
FILE *fp;
char pass[30];
int i;
if(!strcmp(argv[1],"-n")){
printf("Enter apassword :");
scanf("%s",pass);
if(!strcmp(pass,"123"))
{
fp=fopen("Staff.dat","wb");
for(i=1; i<=3; i++)
{
printf("Enter name, age\n");
fseek(fp,record.age*sizeof(struct staff),SEEK_SET);
scanf("%s %d",record.name,&record.age);
fwrite(&record,sizeof(struct staff),1,fp);
}
fclose(fp);
}
else printf("Error password!:");
}
if(!strcmp(argv[1],"-l")){
fp=fopen("Staff.dat","rb+");
for(i=1; i<=3; i++)
{
fseek(fp,record.age*sizeof(struct staff),SEEK_SET);
fread(&record,sizeof(struct staff),1,fp);
printf("%s %d\n",record.name,record.age);
}
fclose(fp);
}
else
printf("Wrong command");
return 0;
}