-1

This is my main file

#include<stdio.h>
#include<string.h>
#include "ssv.c"

int main() {

    int i,length,acct;
    float amnt;
    char data[1000], record[1000];

    FILE *x = fopen("students.ssv","rt");

    while(!feof(x)) {
        for (i = 0; data[i] != '\n'; i++) {
            record[i] = data[i];
            length = i + 1;
        }
        record[length] = '\0';
        parse(record,&acct,&amnt);
        fgets(data,999,x);
    }

    fclose(x);
    return 0;
}

This is my ssv.c file

#include<stdio.h>

void parse(char record[], int *acct, float *amnt){
    sscanf(record,"%d %f",acct,amnt);
}

For some reason, these two programs are not working very well together. I am still getting used to modular programming. I keep getting the messages "undefined reference to main" and "multiple definition of".

My main goal here is to parse a file like

100 -10.5
13 -2.4

into corresponding fields. Please advise!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Missing the important bits like how you are trying to compile/link. – John3136 Nov 21 '19 at 01:10
  • 1
    [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Nov 21 '19 at 01:11
  • You never call `parse()`. And the loop in the main file is using `data` before it has read anything into it. – Barmar Nov 21 '19 at 01:13
  • just updated it – Hidden Dragon Nov 21 '19 at 01:14
  • I fixed your indentation so the program is readable. Why did you undo that? – Barmar Nov 21 '19 at 01:14
  • "I keep getting the messages "undefined reference to main" and "multiple definition of".". Please show us the exact errors and how you are compiling. Makes it difficult if you don't provide all the relevant info. – kaylum Nov 21 '19 at 01:16
  • gcc ssv.c /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status – Hidden Dragon Nov 21 '19 at 01:18
  • Also, I did an fgets(data, 999, x) before the while loop. Sorry about that – Hidden Dragon Nov 21 '19 at 01:18

1 Answers1

2

The first iteration of the while loop uses data before it has read the line. It should be:

    while(fgets(data, sizeof data, x)) {
        parse(data,&acct,&amnt);
    }

You don't need to subtract 1 from the size of data when calling fgets().

There's no reason to copy data to record. You can simply parse data (the parse() function will ignore the newline). fgets() ends the string with a null terminator, you don't need to add it yourself.

#include should only be used for .h files, not .c files. You combine object files using the linker. So get rid of #include "ssv.c", replace it with #include "ssv.h". This file should just contain a declaration of parse().

void parse(char record[], int *acct, float *amnt);

Compile the two programs using:

gcc main.c ssv.c

Or you can compile each file separately then link them:

gcc -c main.c
gcc -c ssv.c
gcc main.o ssv.o
Barmar
  • 741,623
  • 53
  • 500
  • 612