I have just finished a simple C program which is basicaly composed of 3 functions:
accadd(void){}
which adds some customer details to a binary filevoid view(char file[30]){}
which is supposed to let me view some of the data in a.txt file
void modify(char file[30]){}
which verifies if a value of a struct is present in the binary file. If that value exists, it will replace it with the newly inserted one.
Now, I'm pretty new to C (coming from python world - should've learnt them in the opposite order ^__^) and I am sure that there are better ways of improving what I have just written. So I am looking for some advices / tips / improvements in my way of thinking + optimization of the code.
Here is the program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void menu(void);
void accadd(void);
void modify(char file[30]);
void view(char file[30]);
// this should remain as it is
struct date
{
int day;
int month;
int year;
};
// this should also remain as it is
struct customer
{
char name[40], acctype[10];
int accno, age;
double phone;
float amount;
struct date deposit;
} add;
void accadd(void)
{
FILE *fp = fopen("cus.dat", "ab+"); // open the binary file
if (fp == NULL) // making sure that the file is ok
exit(1);
// start adding data
printf("ADD RECORD\n");
printf("Enter today's date(date/month/year) \n");
if (scanf("%d/%d/%d", &add.deposit.day, &add.deposit.month, &add.deposit.year) != 3)
exit(1);
printf("Enter account number\n");
if (scanf("%d", &add.accno) != 1)
exit(1);
printf("Enter customer's name\n");
if (scanf("%s", add.name) != 1)
exit(1);
printf("Enter customer's age\n");
if (scanf("%d", &add.age) != 1)
exit(1);
printf("Enter customer's phone num\n");
if (scanf("%lf", &add.phone) != 1)
exit(1);
printf("Enter the account type(in words): \n\t 1:Current\n\t 2:Saving\n\t 3:Fixed\n");
if (scanf("%s", add.acctype) != 1)
exit(1);
printf("Almost done! Just enter the amount you want to deposit: ");
if (scanf("%f", &add.amount) != 1)
exit(1);
//add data to the binary file
fwrite(&add, sizeof(add), 1, fp);
fclose(fp);
}
// this is pretty straight-forward so I didn't add any comments on it
void view(char file[30])
{
FILE *view, *output;
int test = 0;
output=fopen("output.txt", "w");
fprintf(output,"Customer's List\n");
fprintf(output,"\tCustomer's Name:");
fprintf(output,"\tAccount Number:");
fprintf(output,"\tCustomer's Phone No:\n");
view = fopen(file, "rb");
if (view == NULL)
exit(1);
while (fread(&add, sizeof(add), 1, view) != 0)
{
fprintf(output,"\t%16s", add.name);
fprintf(output,"\t%15d", add.accno);
fprintf(output,"\t%20.0f", add.phone);
fprintf(output,"\n");
test++;
}
fclose(view);
fclose(output);
if (test == 0)
{
printf("NO RECORDS FOUND!\n");
}
else{
printf("Output updated in file output.txt\n");
}
}
void modify(char file[30]){
FILE *view;
int counter = 0;
view = fopen(file, "rb+");
if (view == NULL)
exit(1);
while (fread(&add, sizeof(add), 1, view) != 0)
{
if(add.phone==777777777){
printf("Old phone is: %f",add.phone);
printf("Enter the new phone: ");
scanf("%f", &add.phone);
printf("\nThe new phone is: %f",add.phone);
fseek(view,sizeof(add)*counter,SEEK_SET);
fwrite(&add,sizeof(add),1,view);
}
counter++;
}
fclose(view);
if (counter == 0)
{
printf("NO RECORDS FOUND!\n");
}
else{
printf("add.phone printed: %i\n",counter);
printf("add length %d\n",sizeof(add));
}
}
void menu(void)
{
int n,account_number;
char file[30],account_name[30];
printf("Enter your choice 1, 2, 3\n");
printf("\n1. Insert new data about the customer");
printf("\n2. Output data in output.txt file");
printf("\n3. Modify phone nr.");
printf("\n\nYour choice(1, 2, or 3): ");
while(1){
if (scanf("%d", &n) != 1)
exit(1);
switch (n){
case 1:
accadd();
break;
case 2:
printf("Enter the file name: ");
scanf("%s",&account_name);
view(account_name);
break;
case 3:
modify("cus.dat");
break;
}
printf("Enter your choice 1, 2, 3\n");
printf("\n1. Insert new data about the customer");
printf("\n2. Output data in output.txt file");
printf("\n3. Modify phone nr.");
printf("\n\nYour choice(1, 2, or 3): ");
}
}
int main(void)
{
menu();
return 0;
}