[solved] Declare & Use Gobal Variable in C (Just like Singleton Variable)
-
Hello all. I am trying to re-use a file pointer at the start of the program and then close this when program is shutting down, after some work with it. So far I have come up with this
globals.h
extern FILE* ptrLogFile;
file1.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file2.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file3.c
#include "globals.h"
fclose(ptrLogFile);
But this is not what I am looking for. I want to initialize, using
fopen()
, theptrLogFile
just once (may be inglobals.h
?) and then re-use it infile1.c
andfile2.c
as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? Thanks -
Hello all. I am trying to re-use a file pointer at the start of the program and then close this when program is shutting down, after some work with it. So far I have come up with this
globals.h
extern FILE* ptrLogFile;
file1.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file2.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file3.c
#include "globals.h"
fclose(ptrLogFile);
But this is not what I am looking for. I want to initialize, using
fopen()
, theptrLogFile
just once (may be inglobals.h
?) and then re-use it infile1.c
andfile2.c
as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? ThanksYou have to define and initialise the global variable within one source file:
/* No need to include globals.h here but it does not care if it is included */
FILE* ptrLogFile = NULL;
Then use it anywhere this way:
/* file1.c */
#include "globals.h"if (NULL == ptrLogFile)
ptrLogFile = fopen("RequestsLog.log", "a+");/* file2.c */
#include "globals.h"if (NULL == ptrLogFile)
ptrLogFile = fopen("RequestsLog.log", "a+");/* file3.c */
#include "globals.h"if (NULL != ptrLogFile)
{
fclose(ptrLogFile);
ptrLogFile = NULL;
} -
You have to define and initialise the global variable within one source file:
/* No need to include globals.h here but it does not care if it is included */
FILE* ptrLogFile = NULL;
Then use it anywhere this way:
/* file1.c */
#include "globals.h"if (NULL == ptrLogFile)
ptrLogFile = fopen("RequestsLog.log", "a+");/* file2.c */
#include "globals.h"if (NULL == ptrLogFile)
ptrLogFile = fopen("RequestsLog.log", "a+");/* file3.c */
#include "globals.h"if (NULL != ptrLogFile)
{
fclose(ptrLogFile);
ptrLogFile = NULL;
}Thanks. I got it working. But the log file is not getting updated until I close my program. How do I update the log file even when the program is running?
-
Thanks. I got it working. But the log file is not getting updated until I close my program. How do I update the log file even when the program is running?
-
Hello all. I am trying to re-use a file pointer at the start of the program and then close this when program is shutting down, after some work with it. So far I have come up with this
globals.h
extern FILE* ptrLogFile;
file1.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file2.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file3.c
#include "globals.h"
fclose(ptrLogFile);
But this is not what I am looking for. I want to initialize, using
fopen()
, theptrLogFile
just once (may be inglobals.h
?) and then re-use it infile1.c
andfile2.c
as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? ThanksDon't do it that way. Create a function or library module that does all the work and then just call the actual logging functions anywhere in your code. Call the initialiser, to open the file, at the beginning of your main module, and the closing operation just before your main program terminates.
-
Hello all. I am trying to re-use a file pointer at the start of the program and then close this when program is shutting down, after some work with it. So far I have come up with this
globals.h
extern FILE* ptrLogFile;
file1.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file2.c
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");file3.c
#include "globals.h"
fclose(ptrLogFile);
But this is not what I am looking for. I want to initialize, using
fopen()
, theptrLogFile
just once (may be inglobals.h
?) and then re-use it infile1.c
andfile2.c
as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? ThanksHere's how I deal with global variables. I have one header file, I call Globals.h, with these definitions :
#ifdef DEFINE_GLOBALS
#define Global
#define GlobalVar(Type,Variable,Value) Type Variable=Value
#else
#define Global extern
#define GlobalVar(Type,Variable,Value) extern Type Variable
#endifThen, in one and ONLY one file do this :
#define DEFINE_GLOBALS
#include "include "Globals.h"
#endifbefore I include headers that define variables. Here are some sample definitions :
typedef FILE * PFILE;
GlobalVar( PFILE, PtrLogFile, NULL );
GlobalVar( size_t, LineCount, 0 );