I wrote a program that operates like an ATM.It receives cards(by getting card numbers),define them into database and does withdrawal,deposit,etc.
My program has a defineAccount(FILE* f)
that receives card number,owner name,etc. and writes these information in file Database.txt
.Alse I have a function showCardInformation(FILE* f,int cardNumber)
that shows a information of a card that is defined in file.
When I run the program at first time and save a card information in the file showCardInformation
works correctly but when I run it again I receive run-time error with this function because card owner name in the file has changed to NULL.I have the same problem by another part in my program when card number in the file changes.Why I got this problem?
My code:
typedef enum myBoolean {freezed,notFreezed} Boolean;
typedef struct database
{
int cardNumber;
int password;
char* ownerName;
int accounts;
Boolean accessStatus;
} Database;
void showCardInformation(FILE* f,int cardNumber)
{
system("cls");
puts("This is information of your card:");
fseek(f,sizeof(Database)*(cardNumber-1000),SEEK_SET);
Database load;
fread(&load,sizeof(Database),1,f);
printf("%-15s:%d\n%-15s:%s\n%-15s:%d Rials\n","Card Number",cardNumber,"Owner name",
load.ownerName,"Finance",load.accounts);
return;
}
void defineAccount(FILE* f)
{
system("cls");
puts("You can open a new account here.");
puts("Enter card number:");
scanf("%[\n]");
char cardNumberInput[100];
fgets(cardNumberInput,99,stdin);
//I do some confinement for user input.This is one of the confinements:
for(int index=0;index<strlen(cardNumberInput)-1;index++)
{
if(!isdigit(cardNumberInput[index]))
{
puts("invalid card number.Defining new account failed.");
return;
}//if
}//for
if(strlen(cardNumberInput)!=5)
{
puts("card number must have 4 digits.Defining new account failed.");
return;
}
fseek(f,sizeof(Database)*(atoi(cardNumberInput)-1000),SEEK_SET);
Database load;
fread(&load,sizeof(Database),1,f);
if(load.cardNumber<10000 && load.cardNumber>=1000)
{
puts("This card has been already defined in database.Defining new account failed.");
return;
}
puts("Enter owner name:");
char nameInput[100];
fgets(nameInput,99,stdin);
//confinements
puts("Enter password:");
char passwordInput[100];
fgets(passwordInput,99,stdin);
//confinements
puts("Enter finance(in Rials):");
char financeInput[100];
fgets(financeInput,99,stdin);
//confinements
Database newAccount={atoi(cardNumberInput),atoi(passwordInput),strdup(nameInput),
atoi(financeInput),notFreezed};
fseek(f,sizeof(Database)*(atoi(cardNumberInput)-1000),SEEK_SET);
fwrite(&newAccount,sizeof(Database),1,f);
return;
}
I have initialized the file as follows:
/*
DatabaseFile=fopen("Database.txt","w");
Database blank={0,0,"",0,freezed};
for(int accounts=1000;accounts<10000;accounts++)
{
fwrite(&blank,sizeof(Database),1,DatabaseFile);
}
*/