Help making manipulable variable throught another file

This is a discussion on Help making manipulable variable throught another file within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to make a file to configurate/manipulate variables of a C code.I tried using int and extern int ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    Help making manipulable variable throught another file

    Hello, I'm trying to make a file to configurate/manipulate variables of a C code.I tried using int and extern int throught a header or another C code but it doesn't seems to work (even throught the makefile). I also tried (with XML) to put a data structure to hold the shared variables with the XML_SetUserData function to pass a pointer to this structure to the handlers. Maybe i did something wrong, but i can't find where. Any help is welcome. Thanks.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Do you mean you want to manipulate variables between different programs?
    Devoted my life to programming...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Please post some code we can look at.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Yes it's right.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    OK, you can't share data between separate instances of running programs, simply by declaring variables with the right name.

    Just think of what would happen if it were true.
    I declare
    char *host;
    in my program, and my browser stops working.

    The easy and portable way of sharing data between program instances is to use a file.
    One program writes to the file, and another reads from it.

    Now there is such a thing as shared memory, but how to use it varies depending on which OS you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    code

    So,here is code.h
    Code:
    #ifndef _CODE_H#define _CODE_H#include #include #define X 1typedef unsigned char X_Bool;#define X_TRUE ((X_Bool) 1)#define X_FALSE ((X_Bool) 0)#define X_MODE ((X_Bool) 2)#endif
    then conf.c
    Code:
    int M;M = 2;
    and then code.c
    Code:
    #include #include "code.h"extern int X;extern int M;int main(int argc, char *argv[]) {while (1) {printf("\n%s\n", M);printf("\n%s\n", X);printf("\n%s\n", X_MODE);}
    and then the compile (gcc code.c conf.c -o decode `pkg-config --cflags --libs gstreamer-0.10)I want to change variable throught code.h or conf.c for code.c but it fail. It seems to ignore it with no errors.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Sorry, i can't edit post i don't know why, so i'll write the code again for a better view..h
    Code:
    #ifndef _CODE_H  #define _CODE_H  #include  #include   #define X 1 typedef unsigned char X_Bool; #define X_TRUE ((X_Bool) 1) #define X_FALSE ((X_Bool) 0) #define X_MODE ((X_Bool) 2) #endif
    .c
    Code:
    #include  #include  #include "code.h" extern int X; extern int M; int main(int argc, char *argv[]) {while (1)    {        printf("\n%s\n", M);        printf("\n%s\n", X);        printf("\n%s\n", X_MODE);   } }

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    um...try again!!!

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Code:
    #ifndef _CODE_H  
    #define _CODE_H  
    #include
    #include
    #define X 1typedef 
    unsigned char X_Bool; 
    #define X_TRUE ((X_Bool) 1) 
    #define X_FALSE ((X_Bool) 0) 
    #define X_MODE ((X_Bool) 2) 
    #endif
    Code:
    #include
    #include
    #include "code.h" 
    extern int X;
    extern int M; 
    int main(int argc, char *argv[]) 
     {
      while (1)    
       {        
        printf("\n%s\n", M);
        printf("\n%s\n", X);
        printf("\n%s\n", X_MODE);
       } 
     }
    this is what i see when i fix it....

    but you have 2 blank #include in both header and in code

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Sorry for that. It's #include <stdio.h> and #include <stdlib.h>

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > and then the compile (gcc code.c conf.c -o decode `pkg-config --cflags --libs gstreamer-0.10)I want to change variable throught code.h or conf.c for code.c but it fail
    Are you suggesting that if you edit conf.c, that an existing running instance of 'decode' will magically change it's behaviour?

    If you edit the code, then kill the program, recompile, and re-run it.

    If you want to modify run-time behaviour, you need another approach.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by weavel37 View Post
    I tried using int and extern int throught a header or another C code but it doesn't seems to work (even throught the makefile)
    It sounds like what you're trying to do is to have a "conf" object file that is shared by two separate programs. Normally you define data only in the .c files, and in the .h file you declare it. The "user programs" of that module will then include its header file, like this:

    myconf.h
    myconf.c <-- includes myconf.h

    user1.c <-- includes myconf.h
    user2.c <-- includes myconf.h

    Here is example code for a simple myconf object. You can put as much data in it as you want and it will only need to be recompiled when you change it.

    myconf.h:
    Code:
    #ifndef MYCONF_H
    #define MYCONF_H
    extern int CONF_DATA;
    #endif
    myconf.c:
    Code:
    #include "myconf.h"
    int CONF_DATA = 12345;
    user1.c:
    Code:
    #include <stdio.h>
    #include "myconf.h"
    int main(void)
    {
        printf("Hi, this is user1\n");
        printf("CONF_DATA is %d\n", CONF_DATA);
        return 0;
    }
    user2.c:
    Code:
    #include <stdio.h>
    #include "myconf.h"
    int main(void)
    {
        printf("Hi, this is user2\n");
        printf("CONF_DATA is %d\n", CONF_DATA);
        return 0;
    }
    To produce the object `myconf.o' with gcc, use a command like

    gcc -Wall -c myconf.c

    Then to compile the user1 and user2:

    gcc -Wall -o user1 user1.c myconf.o
    gcc -wall -o user2 user2.c myconf.o

    Usually the Makefile is configured to detect if you change myconf.c (or myconf.h) and automatically issue the first gcc command. Otherwise, only the files that were changed (user1 or user2) need to be re-run.

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    i am thinking what he needs to do, is to have a "data" file, that the program reads and writes to share data, because i dont think after every run, recompiling it would be a user based option...

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Hello, so with the help of other people i did resolve the problem. I have tried a way to not make a lot of files, so I made a simple .cfg like this
    Code:
    VARIABLE=VALUE
    ... /* etc... */
    ...

    and then i do this in the .c


    Code:
    char buf[512], var[512], val[512];
    FILE *fp=fopen("/path/to/configfile", "r");
    
    
    setenv("VARIABLE", "VALUE"); // Set a default
    
    
    while(fgets(buf, 512, fp) != NULL)
    {
            if(sscanf(buf, "%[^=]=%s", var, val) != 2) continue;
            setenv(var, val);
    }
    fclose(fp);
    
    
    g_strdup_printf ("%s", ,getenv("VARIABLE"));

    and then i have "VALUE" show on my screen. And it work ! So thanks all for helping/supporting me .

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, weavel!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making the size of an array variable
    By lfbang87 in forum C++ Programming
    Replies: 37
    Last Post: 03-22-2006, 10:13 AM
  2. Spreading throught the network
    By Korhedron in forum Networking/Device Communication
    Replies: 10
    Last Post: 04-13-2004, 03:44 PM
  3. making a variable with others
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-12-2002, 11:18 PM
  4. Making a Variable-adding function without operator +
    By Magos in forum C++ Programming
    Replies: 0
    Last Post: 09-04-2001, 01:49 PM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21