hi,

Is it possible to load a c file into another c file? I want to give a list of functions used in the c file.

If this isnt possible, can i give this list of functions from a loaded text file, when i copy the used c file into a text file?
Ive already loaded a text file.

tnx

Recommended Answers

All 5 Replies

You could create a header file. That is, take all the function definitions and prototypes from the first C file, put them in another .h file, and then use #include "header_file.h" after you include the standard headers in your second C program. For example
header.h

#include <stdio.h>

void my_printf(const char *message) {

    printf("%s\n", message);
}

main.c

#include <stdlib.h>
#include "header.h"

int main(void) {

    /* Beginning of epic code */
    if(1 == 1)
        my_printf("Hello, World");
    else
        exit(EXIT_FAILURE);
    /* End of epic code */

    return 0;
}

Now, to develop a little on the #include directive, if you use angle brackets after it, the preprocessor looks inside a folder containing all the header files which came with the compiler and replaces the line with the contents of the specified header file. If you use double quotes, on the other hand, the preprocessor scans the path given between the quotation marks. For example, if you wanted to include a file inside a headers folder in the current directory, you would use

#include "headers/file.h"

Other than that, the #include directive works the same in both cases.

I hope this helps.

hi,

Is it possible to load a c file into another c file? I want to give a list of functions used in the c file.

Sure. Open the C file with your new program and read the file a line at a time, looking for the function names. You have to weed out all the reserved words first (while, if, break, etc). Then you have to look at what's left to see if it's a function name or a variable.

tnx for the reactions

I tried to load in the header file but an error comes up:
note: previous definition of 'main' was here and:
error: redefinition of 'main'

I just started working with C so I cant see the problem, I only know I have a double main definition, one in the C file and one in the header file.

this is my header file(copy of the c file):

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

int main()

 {
   FILE* fptr;
  fptr = fopen("data.txt", "r");

  if (fptr == 0)
  {

    printf("Error opening file.");
    return(-1);

  }

  int count = 0;

  int status = 0;
  char temp[50];

  while (status != EOF)
  {

    status = fscanf(fptr, "%50c\n", &temp);
    count++;

  }

  printf("%d\n", count);


  fclose(fptr);

  return(0);

}

AND THIS IS MY CFILE:

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include "headers/header.h"

int main()

 {
   FILE* fptr;
  fptr = fopen("data.txt", "r");

  if (fptr == 0)
  {

    printf("Error opening file.");
    return(-1);

  }

  int count = 0;

  int status = 0;
  char temp[50];

  while (status != EOF)
  {

    status = fscanf(fptr, "%50c\n", &temp);
    count++;

  }

  printf("%d\n" "regels", count);


  fclose(fptr);

  return(0);

}

When this works i have to use printf to give a list of all the used functions in my cfile.
Hope you can help me solve this problem.

Tnx

Use CODE Tags!!!!
No code should ever go into a header file. No variable definitions, either. Why you want to copy your entire c-file into the h-file is beyond me, but that's your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.