I've been working on an assignment where we are given a .ssv file that contains bank accounts in the first column and transactions in the second (20 is adding $20 and -20 is taking away $20), and are asked to create a linked list of the accounts using multiple c file.
There are three in question: main.c (opens the ssv and goes line by line), ssv.c (contains a function for parsing out the data from the fgets() string) and linked.c (creates the linked list and updates the account balances by matching the account number and adding the transaction). They are required to remain separate and cannot be made into .h files.
I've assembled a makefile to put all of them into one executable, but when I run the make, it returns and error. I did some debugging and found that it seems to compile objects files of ssv.c and main.c, but not linked.c, leading to some problems where I use linked.c functions in main.c. I've included each of the file's codes below, as well as the error message that appears when I try to "make". Any help finding the issue would be greatly appreciated.
Error Output:
ctonne2, ~/Projects/COMP206/Assignments/ass6: make
gcc -c linked.c
gcc -c ssv.c
gcc -c main.c
main.c: In function ‘main’:
main.c:21:3: warning: implicit declaration of function ‘parse’; did you mean ‘strsep’? [-Wimplicit-function-declaration]
parse(line,&acct, &amnt);
^~~~~
strsep
main.c:22:3: warning: implicit declaration of function ‘findUpdate’; did you mean ‘initstate’? [-Wimplicit-function-declaration]
findUpdate(acct, amnt);
^~~~~~~~~~
initstate
main.c:24:2: warning: implicit declaration of function ‘prettyPrint’ [-Wimplicit-function-declaration]
prettyPrint();
^~~~~~~~~~~
gcc -o linked.o ssv.o main.o
main.o: In function `main':
main.c:(.text+0x92): undefined reference to `findUpdate'
main.c:(.text+0xb6): undefined reference to `prettyPrint'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'bank' failed
make: *** [bank] Error 1
ctonne2, ~/Projects/COMP206/Assignments/ass6:
makefile:
bank: linked.o ssv.o main.o
gcc -o linked.o ssv.o main.o
linked.o: linked.c
gcc -c linked.c
ssv.o: ssv.c
gcc -c ssv.c
main.o: main.c
gcc -c main.c
main.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void main() {
FILE* ssv = fopen("students.ssv", "rt");
if (ssv == NULL) {
printf("\n File opening failed");
exit(1);
}
int acct = 0;
float amnt = 0;
char line [100], bankAcc[10], balChange[10];
while (fgets(line, sizeof(line), ssv)) {
parse(line,&acct, &amnt);
findUpdate(acct, amnt);
}
prettyPrint();
}
linked.c:
#include <stdio.h>
#include <stdlib.h>
struct ACCOUNT {
int accountNumber;
float balance;
struct ACCOUNT* next;
};
struct ACCOUNT* head = NULL;
void findUpdate(int account, float amount) {
head = (struct ACCOUNT*)malloc(sizeof(struct ACCOUNT));
if (head == NULL) exit (1);
struct ACCOUNT* current = head;
int changed = 0;
while(current->next != NULL) {
if (current->accountNumber == account) {
current->balance = current->balance + amount;
changed++;
}
current = current->next;
}
if ((changed==0)&&(current->next==NULL)) {
struct ACCOUNT* newAccount=(struct ACCOUNT*)malloc(sizeof(struct ACCOUNT));
if (newAccount == NULL) exit(1);
current->next=newAccount;
newAccount->accountNumber = account;
newAccount->balance = amount;
newAccount->next = NULL;
}
}
void prettyPrint() {
struct ACCOUNT *current = head;
while (current!=NULL) {
printf("ACCOUNT ID: %4d BALANCE: $ %7.2f \n",current->accountNumber, current->balance);
current = current->next;
}
}
ssv.c
#include<stdio.h>
#include<stdlib.h>
void parse(char record[], int *acct, float *amnt) {
char *end;
char acctarr[10], amntarr[10];
sscanf (record, "%d %f", acct, amnt);
return;
}
Example .ssv file
10 100.0
20 -50.5
10 -20.0