Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

object - How to create module in C

I need help with modules in C.

I made a little manager system where I save records about school subjects. I have my saveToFile and readFromFile functions in one (main.c) C file. Now I was asked to create a module for reading and writing functions. I created SavingFunctions.c SavingFunctions.h and also I was asked to create an object file .o but I don't get it if I need to write it to myself or just do nothing, because I see some files with .o in my folder. Also, I use structure, maybe I need to put it in a separate file?

SavingFunctions.c

    #include <stdio.h>
    #include <stdlib.h>
    
    #include "SavingFunctions.h"
    
    
    
    
    //READ_FUNCTION
    int numberOfRecords(struct Subjects DataBase[])
    {
    
        FILE *fp = NULL;
        fp = fopen("file.bin", "rb");
    
        if(fp == NULL)
        {
            printf("Error! Failed to open\find the file. 
");
            exit(1);
        }
    
        int i=0;
    
        //Reads the contents of a structure variable from file
        while(fread(&DataBase[i], sizeof(DataBase[i]),1, fp) == 1)
        {
            ++i;
        }
    
        fclose(fp);
    
        return i;
    
    }
    
    
    
    //WRITE_FUNCTION
    void writeTofile(struct Subjects DataBase[], int positionToWrite)
    {
        int recordsNumber;
    
        FILE *fp;
        fp = fopen("file.bin", "wb");
    
        if(fp == NULL)
        {
            printf("Error! Failed to open or find the file.
");
            exit(1);
        }
    
        recordsNumber = 0;
    
        for(int i=0; i<=positionToWrite;++i)
        {
            fwrite(&DataBase[i], sizeof(Subjects), 1, fp);
            recordsNumber++;
        }
    
        fclose(fp);
    
        printf("Total number of items in the file: %d
", recordsNumber);
    
    }


SavingFunctions.h

    typedef struct Subjects
    {
        char Lesson[20];
        char TeachersName[20];
        char TeachersLastName[20];
        int Credits;
        int NumberOfStudents;
    } Subjects;
    
    #ifndef SAVINGFUNCTIONS_H
    #define SAVINGFUNCTIONS_H
    
    
    
    int numberOfRecords(struct Subjects DataBase[]);
    void writeTofile(struct Subjects DataBase[], int positionToWrite);
    
    #endif

question from:https://stackoverflow.com/questions/65859454/how-to-create-module-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The .o files are called object files. If you compile a source file with gcc (without the option -o), an object file with the extension .o will be created at the same location of the source file. To give the object file a specific name, you have to specify the -o option, like:

gcc -c -o module_name SavingFunctions.c

Like noted in the comment section below, you don't have a main function. Therefore you have to add a -c option (for compile). Afterwards you can link the object files together to create a library or an executable (with a main function). Or use the other methods which are mentioned in the other answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...