0

I have defined functions in multiple files. I want to write some text to the same file according to their execution same as below. I have found the way to write execution flow in different in file as below.

function1.h

#ifndef FUNCTION1_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int Sum(int a, int b);

#endif

function1.c

#include "function1.h"

int Sum(int a, int b)
{
    FILE *fp;

    fp = fopen("E:\\tmp\\test.txt", "a");
    fputs("Inside Sum function...\n", fp);
    fclose(fp);

    return a+b;
}

main.c

#include "stdio.h"
#include "function1.h"

int main() {
   int a=10, b=12;
   FILE *fp;

   fp = fopen("E:\\tmp\\test.txt", "a");
   fputs("Before Sum function...\n", fp);
   fclose(fp);

   printf("%d + %d = %d", a, b, Sum(a, b));

   fp = fopen("E:\\tmp\\test.txt", "a");
   fputs("After Sum function...\n", fp);
   fclose(fp);

}

Above solution is very difficult to handle when there are more multiple files. Is there direct way to write test.txt in multiple *.c files?

  • What do you mean by “very difficult to handle” ? What problem are you trying to resolve exactly ? – Paul R Jun 29 '18 at 07:54
  • You could open the file in main and pass the file pointer to the function - after changing the signature - rather than hard code the file name in lots of places. I;m not clear what you would mean by multiple files – doctorlove Jun 29 '18 at 07:56
  • I want to check the execution path. This is a simple example for understanding. If the execution path is working following text should be written the test.txt fiel. Before Sum function... Inside Sum function... After Sum function... – Namodaya Balaarachchi Jun 29 '18 at 07:58
  • Multiple file mean main.c and function1.c. I will try this passing file pointer to the function – Namodaya Balaarachchi Jun 29 '18 at 08:02

3 Answers3

2

Pass file pointer as argument int Sum(int a, int b, File *F) then (at the end) you can lseek to SEEK_SET to go back at beginning of file.

DDS
  • 2,340
  • 16
  • 34
2

You may open a file and pass its descriptor like an argument:

function1.h

#include <stdio.h>
#ifndef FUNCTION1_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int Sum(int a, int b, FILE *f);

#endif

function1.c

#include "function1.h"

int Sum(int a, int b, FILE *f)
{
    fputs("Inside Sum function...\n", f);
    return a+b;
}

main.c

#include "function1.h"

int main() {
   int a=10, b=12;
   FILE *fp;

   fp = fopen("E:\\tmp\\test.txt", "a");
   fputs("Before Sum function...\n", fp);

   printf("%d + %d = %d", a, b, Sum(a, b, fp));

   fputs("After Sum function...\n", fp);
   fclose(fp);

   return 0;
}
Northsoft
  • 171
  • 4
0
#include <stdio.h>
#include <stdlib.h>

/* Define SHOW_WHERE to turn on show_where() */
#define SHOW_WHERE

#ifdef SHOW_WHERE

#define show_where(fp) \
    fprintf(fp, "FILE=%s\tLINE=%d\tFUNC=%s\tDATE=%s\tTIME=%s\n", __FILE__, __LINE__, __FUNCTION__,  __DATE__, __TIME__); \
    fflush(fp);

#else

#define show_where(fp)

#endif

int main(int argc, char *argv[])
{
    show_where(stdout);

    return 0;
}
Joy Allen
  • 402
  • 3
  • 8