Question

I made a this file pointer in my code that should contain my log.txt file's address:

FILE *log

This address depends on the argv[1] from my main.c function, it could be something like this:

char address[200];
strcpy(address, argv[1]);
FILE *log;
log = fopen(address, "w");

I need the address pointed by *log to be visible in all my .c source files, because they will have some lines like this:

fprintf(log, "Comment to be printed on log.txt");

I know it's a bad way to make a log file, but my program is big enough so that changing all the log printing lines will take a considerable amount of time.

The file's address has to be defined using argv[1]. How do I define it before my functions can use it (And how do I make them identify it)?

Was it helpful?

Solution

Define log as a global variable, and declare it as external symbol by extern FILE *log; in other places that you use it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top