Thread: Modular programming in C (Call functions between source file)

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    18

    Modular programming in C (Call functions between source file)

    Ok so I'm having some troubles with understanding modular programming in C. My program is split-up in three parts.

    "main.c" - Code for the menu and function calling. (Vehicle Directory)
    "file.c" Code for the file management. (Read/Write vehicles from/to file)
    "header.h" - Function and struct declarations

    I don't understand how to call functions in "file.c" from "main.c". I did declare the functions in "header.h". Here are the errors I get when compiling with ("gcc main.c file.c -o prog")

    main.c:2:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]skriv_in();
    ^
    main.c:2:1: error: conflicting types for 'skriv_in'
    ./header.h:26:6: note: previous declaration is here
    void skriv_in();
    ^
    1 warning and 1 error generated.
    file.c:6:20: error: function definition is not allowed here
    void skriv_in(){
    ^
    file.c:25:24: error: function definition is not allowed here
    void skriv_ut(){

    ^
    header.h
    Code:
    #ifndef HEADER_H_#define HEADER_H_
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    /* no magic numbers */
    #define ANTAL_MAX_FORDON 10
    #define AVSLUTA 6
    static const int MAX_ALLOWED_LENGTH = 20;
    
    /* Vehicle Data */
    typedefstruct fordon {
      char namn[MAX_ALLOWED_LENGTH];
      char marke[MAX_ALLOWED_LENGTH];
      char fordonstyp[MAX_ALLOWED_LENGTH];
      char registeringsnummer[MAX_ALLOWED_LENGTH];
    } fordon;
    fordon f[ANTAL_MAX_FORDON];
    
    bool add(fordon * f);
    bool delete(fordon * f);
    void sortera();
    
    void skriv_in();
    void skriv_ut();
    
    #endif                          // HEADER_H_
    main.c
    Code:
    #include "header.h"
    skriv_in();
    
    /* Function remove vehicle */
    bool delete(fordon * f)
    {
      fordon tmp;
      int i = 0;
    
      memset(&tmp.namn[0], 0, sizeof(tmp.namn));
      memset(&tmp.marke[0], 0, sizeof(tmp.marke));
      memset(&tmp.fordonstyp[0], 0, sizeof(tmp.fordonstyp));
      memset(&tmp.registeringsnummer[0], 0, sizeof(tmp.registeringsnummer));
    
      *f = tmp;
      returntrue;
    }
    
    /* Function add vehicle */
    bool add(fordon * f)
    {
      fordon tmp;
      printf("Skriv in namn: ");
      scanf("%s", tmp.namn);
      printf("Skriv in märke: ");
      scanf("%s", tmp.marke);
      printf("Skriv in fordonstyp: ");
      scanf("%s", tmp.fordonstyp);
      printf("Skriv in registeringsnummer: ");
      scanf("%s", tmp.registeringsnummer);
    
      *f = tmp;
      returntrue;
    }
    
    /* function sort by car brand */
    void sortera()
    {
      int i, j;
    
      fordon temp;
    
      for (int i = 0; i < ANTAL_MAX_FORDON; i++) {
        for (int j = i + 1; j < ANTAL_MAX_FORDON; j++) {
          if (strcmp(f[i].marke, f[j].marke) > 0 && f[j].marke[0] != '\0') {
            temp = f[i];
            f[i] = f[j];
            f[j] = temp;
          }
        }
      }
    }
    
    /* main - switch menu with safe input */
    int main()
    {
      int val = 1, i = 0, nummer = 0;
      char st[1024];
    
      while (nummer != AVSLUTA) {
    
        printf("\n1. Lägg till fordon");
        printf("\n2. Ta bort fordon");
        printf("\n3. Sortera efter bilmärke");
        printf("\n4. Skriv ut information om ett fordon");
        printf("\n5. Skriv ut hela fordonregistret");
        printf("\n6. Avsluta\n");
        printf(" Skriv in ett val(1-6) ");
    
        /* Kod för säker inmatning */
        fgets(st, sizeof(st), stdin);
        val = sscanf(st, "%d", &nummer);
        if (val != 1) {
          printf("\nFel! Välj något av alternativen 1-6.\n");
        } else {
    
          /* switch meny */
          switch (nummer) {
          case 1:
            i = 0;
            while (f[i].namn[0] != '\0' && i < ANTAL_MAX_FORDON) {
              i++;
            }
            printf("(%d)", i + 1);
            if (f[9].namn[0] != '\0') {
              printf("\n Registret är fullt\n");
              break;
            } else {
              add(&f[i]);
              break;
            }
            break;
          case 2:
            i = 0;
            printf("\nSkriv in platsen(1-10) på det fordon du vill ta  bort: ");
            scanf("%d", &i);
            delete(&f[i - 1]);
            break;
          case 3:
            sortera();
            break;
          case 4:
            i = 0;
            printf
                ("\nSkriv in platsen(1-10) på det fordon du vill skriva ut information om: ");
            scanf("%d", &i);
            if (i < 1 && i > ANTAL_MAX_FORDON) {
              printf("\nFel! Välj någon av platserna 1-10.\n");
            } else if (f[i - 1].namn[0] == '\0') {
              printf("\nPlatsen är tom.\n");
            } else {
              printf("%d.%s    %s    %s    %s\n", i, f[i - 1].namn, f[i - 1].marke,
                     f[i - 1].fordonstyp, f[i - 1].registeringsnummer);
            }
            break;
          case 5:
            for (i = 0; i < ANTAL_MAX_FORDON; i++) {
              printf("%d.%s    %s    %s    %s\n", i + 1, f[i].namn, f[i].marke,
                     f[i].fordonstyp, f[i].registeringsnummer);
            }
            break;
          case 6:
            skriv_ut();
            break;
    
          default:
            printf("\nFel! Välj något av alternativen 1-6.\n");
          }
        }
      }
    
      return 0;
    }

    file.c
    Code:
    #include "header.h"
    
    int filh()
    {
    
      void skriv_in() {
        int i = 0;
        /* Read from file */
        FILE *file1, *file2;
        file1 = fopen("labb4.txt", "r");
        if (file1 == NULL) {
          file1 = fopen("labb4.txt", "w+");
        } else {
          while (!feof(file1)) {
            for (i = 0; i < ANTAL_MAX_FORDON; i++) {
              fscanf(file1, "%s %s %s %s", f[i].namn, f[i].marke, f[i].fordonstyp,
                     f[i].registeringsnummer);
            }
            fread(&f, sizeof(f), 1, file1);
          }
        }
        fclose(file1);
      }
    
      /* Write to file */
      void skriv_ut() {
        FILE *file1, *file2;
        int i = 0;
        file2 = fopen("labb4.txt", "w+");
        char buf[1024];
        int needle = 0;
        memset(buf, 0, 1024);
        for (i = 0; i < 10; ++i) {
          if (f[i].namn[0] != '\0' || f[i].marke[0] != '\0'
              || f[i].fordonstyp[0] != '\0' || f[i].registeringsnummer[0] != '\0') {
            needle =
                needle + sprintf(buf + needle, "%s %s %s %s\n", f[i].namn,
                                 f[i].marke, f[i].fordonstyp,
                                 f[i].registeringsnummer);
          }
        }
        fwrite(buf, strlen(buf), 1, file2);
        fclose(file2);
      }
      return0;
    }
    Last edited by Heisenberg800; 09-17-2018 at 12:45 AM. Reason: Typo

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Get rid of line 2 in main.c


    Next time you post code, make sure to either
    - select copy as text in your IDE
    - select paste as text in your browser.

    The eyesore candy store you posted is horrible.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by Salem View Post
    Get rid of line 2 in main.c


    Next time you post code, make sure to either
    - select copy as text in your IDE
    - select paste as text in your browser.

    The eyesore candy store you posted is horrible.
    Ok sorry will try to edit that out.

    Line 2 in main.c is my attempt to call the function that reads in from file to the directory. The problem is that that function is in the other source file. So my compiler thinks that I want to declare the function when in fact I want to call it. I guess my question is how can I call something from file.c to main.c?

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    18
    Quote Originally Posted by Salem View Post
    Get rid of line 2 in main.c
    I solved my own problem. The line 2 in main.c was supposed to be placed inside "int main()".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. Modular Programming Help
    By trintiin in forum C Programming
    Replies: 3
    Last Post: 03-19-2013, 05:34 PM
  3. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  4. Want to put Functions in another source file
    By jjj93421 in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2004, 12:50 AM
  5. Modular programming
    By Longie in forum Linux Programming
    Replies: 1
    Last Post: 07-08-2003, 11:03 PM

Tags for this Thread