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:
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?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);
}

