-
Bank like system
Hey,
I am trying to make a system as a class assignment. I have only started but I am unsure of how to do one piece of code already. I have:
Code:
//Files using CSV Format
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct customer
{
int accno;
char name[25];
char address[50];
};
typedef struct customer C;
void newperson( C * cArray);
void printperson( C * cArray);
#define CUSTOMERS "customers.txt"
void main()
{
C * cArray;
int numPersons=0;
cArray = ( C * ) malloc( 1 * sizeof( C ) );
system("COLOR 1E");
newperson(cArray);
printperson(cArray);
printf("\n\nWritten to file\n\n");
}
void newperson( C * cArray)
{
char ch;
FILE * customerfile = fopen(CUSTOMERS, "r");
if (customerfile==NULL)
exit(errno);
printf("Enter name:\n");
gets( (cArray )->name);
printf("Enter address:\n");
gets( (cArray )->address);
printf("\nPress return to write information...\n");
ch = getchar();
fclose(customerfile);
}
void printperson( C * cArray)
{
FILE * customerfile = fopen(CUSTOMERS, "a");
if (customerfile==NULL)
exit(errno);
printf("Name\t\tAddress");
printf("\n%d", (cArray)->accno);
printf("\t%s", (cArray)->name);
printf("\t\t%s", (cArray)->address);
fprintf(customerfile,"%s,%s\n", cArray[0].name, cArray[0].address);
fclose(customerfile);
}
This much is working. What I want to be able to do is set up a counter so that accno will increment itself by 1 each time a new account is written to the file. Anyone any guidance on how to go about this?
-
Well first you will want to read how many accounts are in the file. A quick and easy way to do this could be to get the number of newlines then divide it by however many lines the data for each account uses.
-
If there is no one added yet how do i work around this? Should I just hardcode a sample customer to file to overcome this?
-
After opening the file test if the file pointer is null, if so the file does not exist so set the count to 0. Alternatively if a file exists but is empty then you count will finish at 0.
-
Agh, you are using void main I see. And gets too!
Void main is undefined and gets is dangerous. Read:
http://cpwiki.sourceforge.net/Gets
and
http://cpwiki.sourceforge.net/Void_main
Try fixing those first.
As for the count, well... you need to keep track of it. So you could write it at the beginning of your file.
When the program runs, read back that number and increase it by one for every new account and write the new number to file.
-
I'm trying a slightly diff approach now but keep getting an error early on. Help appreciated again
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct customer
{
int accno;
char name[25];
char address[50];
};
typedef struct customer C;
void loaddata(C * cArray);
void readperson( C * cArray);
void printperson( C * cArray);
#define CUSTOMERS "customer.txt"
#define LINE_LENGTH 80 //Maximum buffer size for reading in file.
int main(void)
{
C * cArray;
int numPersons=0;
cArray = ( C * ) malloc( 1 * sizeof( C ) );
loaddata(cArray);
readperson(cArray);
printperson(cArray);
printf("\n\nWorking\n\n");
}
void readperson( C * cArray)
{
char ch;
printf("Enter name:\n");
gets( (cArray )->name);
printf("Enter address:\n");
gets( (cArray )->address);
printf("\nPress any key to continue...\n");
ch = getchar();
}
void printperson( C * cArray)
{
FILE * customerfile = fopen(CUSTOMERS, "a");
if (customerfile==NULL)
exit(errno);
printf("Name\t\tAge");
printf("\n%s", (cArray)->name);
printf("\t\t%s", (cArray)->address);
fprintf(customerfile,"%s,%s\n", cArray[0].name, cArray[0].address);
fclose(customerfile);
}
void loaddata(C * cArray)
{
FILE * customerfile = fopen(CUSTOMERS, "r");
char tmpline[LINE_LENGTH];
int linecount=0; //Used as counter for number of lines
int k = 0;
if (customerfile==NULL)
exit(errno);
linecount = 0;
while (fgets(tmpline,LINE_LENGTH,customerfile) != NULL)
linecount++;
*cArray = malloc(linecount * sizeof(C));
if (cArray==NULL)
exit(errno);
rewind(customerfile);
printf ("Data loaded\n");
}
-
Were you not already corrected about gets?
http://cpwiki.sourceforge.net/gets
-
I was but its working for time being so i want to try get something happening. can rectify gets then. Please help and i promise once i have time to learn new code i will never use gets again!
-
I think you also failed to mention what is wrong.
Plus changing gets isn't difficult.
fgets(buffer, sizeof(buffer), stdin)
That's all.
So
gets( (cArray )->name);
becomes
fgets(cArray->name, sizeof(cArray->name), stdin);
Code:
*cArray = malloc(linecount * sizeof(C));
This is incredibly wrong.
You dereference the actual cArray pointer and store the address to the allocated memory there.
Hmmm. How do you assign void* to customer?
And after that... you aren't even reading the data from the file into the array.
Yet later you're actually asking for new data and put it into your struct, making the whole previous loaddata useless.
And you're just writing one entry at the end of the file, as well.
What are you trying to accomplish?
-
What i aim to do is that when i start the program it will read in the file of customers and see how many are there. when it has that done i want to add a customer +1 from the last customer but im getting stranded at it!
-
Add another customer you say... yet what are you going to do with all those? Why do you need to know how many customers there are?
-
i will hopefully be puttin in a binary search to find customers so need it for that. i will also need to know for when the program is run several times how many customers are already there to increment the account number by 1 for the next customer.
-
You obviously need to dynamically allocate memory, so you should pass a pointer to pointer to your function to allocate the memory you need and then read the file into that array.
There are two approaches to this. Either you can read through the entire file and count the lines and then fill the array or you can allocate n amount of structs, read n amount of structs and if it isn't enough, reallocate your array to hold more structs and continue reading.
Once you have all of them in your big array, you can do whatever with it.
-
I want to do the first option where I read the lines.
Is my code pure rubbish thus far so? The whole project I will want to have users who can have one of 3 types of account. They will be able to lodge or withdraw money from these account and get a statement. If I can get the accounts working including the account number i have a fair idea how to do the accounts. (I think)
-
No, it just needs a little modification, that's all. I don't know if you think you can implement a double pointer system?
The reason is that main also needs access to your dynamic buffer, so you must pass a pointer to that pointer and allocate memory in it.
Give it a try.