Hello everyone,
I am sort of new in C programming and since I programmed mostly in C++, I may have some mix up on my program which is giving me some errors. I have written a small code , in modular fashion i.e. I have split the .h, definition.c , and main.c files.
The program currently has only one function and all that does is read from a text file. My problem is when I compile each file separately I get the error: 'Undefined reference to ReadFromfFile()" (Note: ReadFromFile is a function defined in definition.c and I get this error when I compile the main.c file)
And when I compile the definition.c file I get the error 'Undefined reference to main'.
And when I compile all my files together as such: gcc DeliveryServices.h DeliveryServices.c main.c -o Servrices: I get the error 'Segmentation fault(core dumped)'.

Please someone point me out what am I doing wrong and how can I fix it.

Thank you.
//Header File

#ifndef DeliveryServices_H
#define DeliveryServices_H


#include <stdio.h>
#include <string.h>

 struct DeliveryServices{
                        char* ItemType;
                        char* PickUpAdd;
                        char* DropOffAdd;
                      };
                      typedef struct DeliveryServices ServiceSlip;
#endif   

//Definition File

#include "DeliveryServices.h"
#include <stdio.h>
#include <string.h>



char ReadFromFile()
{
        FILE *ReadFrom;
        char lineRead;

        printf("Hello");
        ReadFrom = fopen("datafile.txt","r");
        while(fgets(lineRead,120,ReadFrom))
        {
                printf("%s",lineRead);
        }
return 0;
}

//Main.c

#include "DeliveryServices.h"
#include <stdio.h>
#include <string.h>

int main()
{       
        FILE *ReadFile;
        char lineRead;

        ServiceSlip  PackageSlips;


        ReadFromFile();
        //PackageSlips.ItemType ="Package";
        //printf("%s",PackageSlips.ItemType);

return 0; 
}

My problem is when I compile each file separately I get the error: 'Undefined reference to ReadFromfFile()" (Note: ReadFromFile is a function defined in definition.c and I get this error when I compile the main.c file)

Your compile is also linking, and definition.c was not specified.

And when I compile the definition.c file I get the error 'Undefined reference to main'.

Same.

In both cases, if you compile only it should work. But why bother. Linking is the magic.

And when I compile all my files together as such: gcc DeliveryServices.h DeliveryServices.c main.c -o Servrices: I get the error 'Segmentation fault(core dumped)'.

You don't specify the .h files when compiling. The compiler will find it because of the #include statement.

this is the link on howto do it

try to compile hello world program first maybe error is in your code ? cause char* ItemType; is pointer to memmory did you allocate some memmory for it ?

and you read to char lineRead; which is actually one byte long, you read there 120 bytes
it should be then char lineRead[120]

'Segmentation fault(core dumped)'. means memmory error

Edited 4 Years Ago by Sokurenko: fixed

Have you included a prototype for ReadFromFile() in the header file for main.c? Something like:

char ReadFromFile(void);

Thank you every one for your suggestions. I looked into my program more carefully and rewrote some stuff and it has started to work but I have another (smaller) problem.

My issue is: I have a text file as such:

Address 1
Address 2
Package
0
Address 3
Address 4
Package
1

My program reads the first para (i.e. till 0) but does not read any further. I tried a do while loop but that did not make any difference. How else should I approach this.

I have rewrote the definition.c (for readfile) file. Here is my code again:

//FunctionDefinition.h file

#include "DeliveryServices.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>





char ReadFromFile()
{
    FILE *ReadFrom;
    char *buffer;
    char *tokenizer;
    unsigned long  fileLength;

    ServiceSlip PackageSlips;

    ReadFrom = fopen("datafile","r");

    if(!ReadFrom)
    {
        printf("Error opening file \n");
    }
    else
    {

    fseek(ReadFrom,0,SEEK_END);
    fileLength = ftell(ReadFrom);
    fseek(ReadFrom,0,SEEK_SET);
    }
    buffer =  (char*)malloc(fileLength+1);
    if(!buffer)
    {
        printf("Errom allocating memory");
    }
    fread(buffer,fileLength,1,ReadFrom);

    //printf("%s \n",buffer);


    tokenizer = strtok(buffer,"\n");
    strcpy(PackageSlips.PickUpAdd,tokenizer);

    tokenizer = strtok(NULL,"\n");
    strcpy(PackageSlips.DropOffAdd,tokenizer);

    tokenizer = strtok(NULL,"\n");
    strcpy(PackageSlips.ItemType,tokenizer);

    tokenizer = strtok(NULL, "\n");
    strcpy(PackageSlips.PriorAssign,tokenizer);


    printf("%s \n",PackageSlips.PickUpAdd);
    printf("%s \n", PackageSlips.DropOffAdd);
    printf("%s \n",PackageSlips.ItemType);
    printf("%s \n",PackageSlips.PriorAssign);


    fclose(ReadFrom);

return 0;
}


//main file
#include "DeliveryServices.h"
#include <stdio.h>
#include <string.h>

int main()
{   
    FILE *ReadFile;
    char lineRead;

    ServiceSlip  PackageSlips;


    ReadFromFile();


return 0; 
}

Thanks for all your help guys.

have you tryed this ? http://www.cplusplus.com/reference/clibrary/cstring/strtok/

while (tokenizer != NULL)
{
tokenizer = strtok(buffer,"\n");
//check if it is not NULL then strcpy else break;
strcpy(PackageSlips.PickUpAdd,tokenizer);
tokenizer = strtok(NULL,"\n");
strcpy(PackageSlips.DropOffAdd,tokenizer);
tokenizer = strtok(NULL,"\n");
strcpy(PackageSlips.ItemType,tokenizer);
tokenizer = strtok(NULL, "\n");
strcpy(PackageSlips.PriorAssign,tokenizer);
}

Edited 4 Years Ago by Sokurenko: code snipper

Why are you doing all that crap with tokens, fseeks, and stuff?

What's wrong with:

open file
while not EOF
   read addressA
   read addressB
   read package
   read number
   process data read
end-of-while
finish the program

Edited 4 Years Ago by WaltP

Quoted Text Here

Why are you doing all that crap with tokens, fseeks, and stuff?

What's wrong with:....

Because this is how my text file looks:
1013 Richmond Ave ("this is Address1")
2034 Downtown Ave ("this is Address2")
Package
0

When I use tokenizer, it does all the work for me. In other words, with tokenizer "Address 1" gets stored in variable Address 1, "Address 2" gets stored in variable Address 2 and so on.
But with the method you showed me. this is the output I will get:
1013
Richmond
Ave
2034
Downtown
Ave
...
In other words every word gets stored separately.

Then you are using the wrong 'read' command. Don't read a word at a time, read a line. Try getline()

This article has been dead for over six months. Start a new discussion instead.