[prog] Bug in prgm

Sachin sachinr at vsnl.in
Sat Feb 8 23:44:06 EST 2003


Dear ladies and gentlemen,


I should thank all the people in the group for spending their valuable time
and helping  me.I just need a help from you .Can anyone tell me what is
wrong with the source code.I've been battling with this for one whole day.

Sachin.R.Athreya

[Description:        This program opens a file "keyfile " and checks for a
name say sachin(performed by chkrcd( ) which returns -1 if the name not
found and the value attached with the name if the name is present.The
function setrcd( ) again finds the name and   modifies according to the
"number" given at the command line parameter.]
[Last bug discovered : The file overwrites itself ]

#include<stdlib.h>
#include<string.h>
#include<stdio.h>
typedef struct
{
char name[30];
unsigned long int last_rcd_acsd;
}keyfile;
int chkrcd(char *username);
void setrcd(char *username,unsigned long int number);
int chkrcd(char *username)
{
FILE *openrcd;
keyfile kfl;
int status,glo_count,loc_count;
char tempchar[10];
printf("\n -----------------------------------------------------------");
printf("\n IN THE FUNCTION CHKRCD ");
printf("\n -----------------------------------------------------------");
openrcd=fopen("keyfile","r");

status=fread(&kfl,sizeof(keyfile),1,openrcd);
printf("\nThe value of status returned by read in chkrcd:%d",status);
glo_count=loc_count=0;
while(status!=0 && !feof(openrcd))
{
printf("\nThe value of kfl.name:%s and the username:%s",kfl.name,username);
if(strcmp(kfl.name,username)==0)
{
fclose(openrcd);
return kfl.last_rcd_acsd;
}
status=fread(&kfl,sizeof(keyfile),1,openrcd);
printf("\nWhat is the value of status in read in loop in chkrcd:%d",status);
}
fclose(openrcd);
return -1;
}
void setrcd(char *username,unsigned long int number)
{
FILE *openrcd;
int status,glo_count,loc_count,prevposition;
fpos_t position;
keyfile kfl;
char *filename;
printf("\n ------------------------------------------------------------");
printf("\n IN THE FUNCTION SETRCD ");
printf("\n ------------------------------------------------------------");
filename=(char *)malloc(strlen("keyfile")+1);
strcpy(filename,"keyfile");
openrcd=fopen(filename,"a");
rewind(openrcd);
prevposition=0;
status=fread(&kfl,sizeof(keyfile),1,openrcd);
printf("\nThe value of status in setrcd before entering into the
loop:%d",status);
while(status>0 && !feof(openrcd))
{
printf("\nkfl.name:%s",kfl.name);
if(strcmp(kfl.name,username)==0) {printf("\nGoing out of loop");goto
out_of_loop;}
prevposition=fgetpos(openrcd,&position);
status=fread(&kfl,sizeof(keyfile),1,openrcd);
printf("\nThe amount of bytes read by sys call in the loop:%d",status);
}
out_of_loop:
fseek(openrcd,-sizeof(kfl),SEEK_CUR);
strcpy(kfl.name,username);
kfl.last_rcd_acsd=number;
status=0;
status=fwrite(&kfl,sizeof(keyfile),1,openrcd);
printf("\nWhat happened to write in setrcd:%d",status);
fclose(openrcd);
}
int main()
{
char username[30];
int number,status;
printf("Enter the user name:");
scanf("%s",username);
printf("\nEnter the number:");
scanf("%d",&number);
status=creat("keyfile",0644);
close(status);
status=chkrcd(username);
printf("\nThe value of status in main function returned by
chkrcd:%d",status);
if(status==-1) setrcd(username,1);
else setrcd(username,status+10);
printf("The last of records written:%d",chkrcd(username));
return 0;
}




More information about the Programming mailing list