Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem linking files with headers, C

$
0
0
Alright So I have a few functions spread across a few files and I am attempting to get them to be called by my main and run properly but I keep getting issue that I'm not so sure how to deal with.

I have a main:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "construct.h"
#include "sort.h"

#define MAX_NAME 16
#define MAX_SUBSEC 4
#define N 128
#define DELIM "=,\""

struct student{

	int term;
	int id;
	char lastname[MAX_NAME];
	char firstname[MAX_NAME];
	char subjectname[MAX_SUBSEC];
	int catalog;
	char section[MAX_SUBSEC];

};

void printStructs(struct student *list, int size);

int main(){

    struct student students[N];

    int studentsRead = 0;

	int term;
	int id;
	char lastname[MAX_NAME];
	char firstname[MAX_NAME];
	char sub[MAX_SUBSEC];
	int cat;
	char sec[MAX_SUBSEC];

    char fname[N];
    FILE *inputf;

    printf("Enter the name of the text file: ");
    scanf("%123s",fname);

    strcat(fname,".txt");

    inputf = fopen(fname,"r");

    if (inputf == NULL){

         printf("Couldn't open the file for reading.\n");
         exit(0);

	}

    studentsRead = readStudentList(inputf, students, N);

    sortStruct(students, studentsRead);

    printStructs(students, studentsRead);

    fclose(inputf);

    return 0;

}

void printStructs(struct student *list, int size){

    int j = 0;
    char buffer[35];

    printf("Last Name, First Name            Term      ID      Course   Section\n");
    printf("-------------------------------- ----  ---------   ------   -------\n");

    while(j < size){

        sprintf(buffer, "%s, %s", list[j].lastname, list[j].firstname);

        printf("%-32s %-5d %-11d %s%d %7s\n", buffer, list[j].term, list[j].id, list[j].subjectname,list[j].catalog, list[j].section);

        j++;

    }
}


A file called construct.c:
#include "construct.h"

int readStudentList(FILE *f, struct student *list, int maxSize){

    char buff[65];
    int count = 0;

    while((count < maxSize) && (fgets(buff, sizeof(buff), f) != NULL)){

        struct student item;

        if(parseStudent(&item, buff)){

            list[count++] = item;

        }
    }

    return count;

}

int parseStudent(struct student *person, char *data){

    char *ptr;

    int i = 0;

    ptr = strtok(data, DELIM);

	person[i].term = atoi(ptr);

	ptr = strtok(NULL, DELIM);

    person[i].id = atoi(ptr);

	ptr = strtok(NULL, DELIM);

	strcpy(person[i].lastname, ptr);

	ptr = strtok(NULL, DELIM);

	strcpy(person[i].firstname, ptr);

	ptr = strtok(NULL, DELIM);

	strcpy(person[i].subjectname, ptr);

	ptr = strtok(NULL, DELIM);

	person[i].catalog = atoi(ptr);

	ptr = strtok(NULL, DELIM);

	strcpy(person[i].section, ptr);

}


Its header file, construct.h:
int readStudentList(FILE *f, struct student *list, int maxSize);
int parseStudent(struct student *person, char *data);


sort.c (which doesn't work for some reason?):
#include "sort.h"

void sortStruct(struct student *list, int studentCount){

    int j, k;

    struct student temp;

    char buffer[35];
    char buffer2[35];

    for (j = 0 ; j <= studentCount-2 ; j++){

        sprintf(buffer, "%s, %s", list[j].lastname, list[j].firstname);

        for(k = 1 ; k <= studentCount-1 ; k++){

            sprintf(buffer2, "%s, %s", list[k].lastname, list[k].firstname);

            if(buffer < buffer2){

                temp = list[j];
                list[j] = list[k];
                list[j] = temp;

            }
        }
    }
}


And finally its header, sort.h:
void sortStruct(struct student *list, int studentCount);


When ever I attempt to build I get a lot of errors mostly of unknown type names inside the headers and other variables and defines belonging to the main that the other source files won't pick up on. I haven't moved or touched any files or put things in different directories. I also went into the projects build properties and set the search directories for the compiler and linker to my project folder. What do!?

Ex. construct.h|2|error: unknown type name 'FILE'|
construct.c|4|error: unknown type name 'FILE'|
construct.c|24|error: conflicting types for 'parseStudent'|
.
.
.
etc.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>