I'm running on a MAC. I'll post a portion of the cod
OPTION TO ADD A NEW RECORD
Code:
int accountno;
char name[25];
char address[80];
int addresscap = 79;
int yearofbirth;
char yearinput[10];
char info[100];
char filename[] = "data.txt";
printf("Please create your own account number: ");
scanf("%s",info);
while(!isInteger(info,99)){
printf("\nThat is not a valid account number. Only integers are allowed.\n");
printf("Please create your own account number:");
scanf("%s",info);
}
accountno = atoi(info);
clear(); //removes lingering '\n'
printf("\nPlease enter your name: ");
fgets(name, 24, stdin);
printf("\nPlease enter your address:\n");
printf("To finish your input, make a new line after your input and enter 'quit':\n");
getField(address,addresscap); //Reads multiple lines until user inputs 'quit'
printf("\nPlease enter your year of birth: ");
scanf("%s",yearinput);
while(strlen(yearinput) != 4 || !isInteger(yearinput,10)){
printf("\nNot a valid year of birth. Please try again: \n");
scanf("%s",yearinput);
}
yearofbirth = atoi(yearinput);
addRecord(&start,accountno,name,address,yearofbirth); //Adds into singly linked list
READ FILE CODE
Code:
FILE *fp = NULL;
fp = fopen(filename,"r");
int accountno = 0;
char name[25] = { 0 };
char address[80] = { 0 };
int yearofbirth = 0;
char line[80] = { 0 };
int opened = 1;
if (fp == NULL){
opened = 1;
} else {
opened = 0;
while (fscanf(fp, "%d\n", &accountno) > 0) {
printf("accountno: %d\n",accountno);
fgets(name, 25, fp);
while (fgets(line, 80, fp)) {
if (strcmp(line, "*\n") == 0) {
strncpy(line,"",sizeof(line)-1);
break;
}
strncat(address, line, 79 - strlen(address));
}
fscanf(fp, "%d", &yearofbirth);
addRecord(start, accountno, name, address, yearofbirth); //Reads each piece of data in the file and adds it to the linked list
strncpy(line,"",sizeof(line)-1); // Used these to try to clear out the array
strncpy(address,"",sizeof(address)-1);
strncpy(name,"",sizeof(name)-1);
}
}
if (opened != 1){
fclose(fp);
}
return opened;
}