Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [solved] Declare & Use Gobal Variable in C (Just like Singleton Variable)

[solved] Declare & Use Gobal Variable in C (Just like Singleton Variable)

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    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(), the ptrLogFile just once (may be in globals.h?) and then re-use it in file1.c and file2.c as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? Thanks

    J L R 3 Replies Last reply
    0
    • D Django_Untaken

      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(), the ptrLogFile just once (may be in globals.h?) and then re-use it in file1.c and file2.c as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? Thanks

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      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;
      }

      D 1 Reply Last reply
      0
      • J Jochen Arndt

        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;
        }

        D Offline
        D Offline
        Django_Untaken
        wrote on last edited by
        #3

        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?

        J 1 Reply Last reply
        0
        • D Django_Untaken

          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?

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Call fflush[^] after writing to the stream.

          1 Reply Last reply
          0
          • D Django_Untaken

            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(), the ptrLogFile just once (may be in globals.h?) and then re-use it in file1.c and file2.c as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? Thanks

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Don'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.

            1 Reply Last reply
            0
            • D Django_Untaken

              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(), the ptrLogFile just once (may be in globals.h?) and then re-use it in file1.c and file2.c as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? Thanks

              R Offline
              R Offline
              Rick York
              wrote on last edited by
              #6

              Here'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
              #endif

              Then, in one and ONLY one file do this :

              #define DEFINE_GLOBALS
              #include "include "Globals.h"
              #endif

              before I include headers that define variables. Here are some sample definitions :

              typedef FILE * PFILE;

              GlobalVar( PFILE, PtrLogFile, NULL );
              GlobalVar( size_t, LineCount, 0 );

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups