-3

Header file

struct some_variable {
char *variable;
uint32_t infoe;
uint8_t *info0;
};

1.c in some directory

function1:
----------

static void filename(const char *variable, 


function2:
----------

int read_variable(some_variable *var)

    FILE *f = NULL;
    f = fopen(filename, "rb");
    .
    .
    .
    .
}

2.c in other directory

function3:
----------

int own_function()
{
    char buf[256];
    uint8_t cam[3];
    struct some_variable var;

    var.variable = "iop";

    if (strncmp(var.variable, "iop", 3) == 0) {
        read_variable(&var);
        f = fopen(filename,"r"); 

        while (fgets(buf, sizeof buf, f)) {     
            sscanf(b, "%hhX:%hhX:%hhX:"
                    &cam[0], &cam[1], &cam[2]);
            ....
    }
}
  • function1 and function2 are in one file in some directory, function3 is in another file which I am writing.
  • I called function 2 in function3.
  • I would like to use "filename" from function2 in function3. note: I can't change function2'
Harry M
  • 1
  • 2

2 Answers2

1

You can't.

The FILE handle, f is a local variable of read_variable. Hence, it's completely inaccessible to anything outside of read_variable.

You didn't show the complete code to read_variable, but given that it opens the file handle into a local variable, I would expect it to also be invoking fclose before it returns.

If you are not permitted to modify read_variable, have you considered just copying the entire code for read_variable into your own_function call and modifying it to meet your needs?

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks for responding! In function2, After fopen it reads (fread) the variable and then just do fclose(f) I tried to copy function2 in function3 and everything worked fine. But I am not allowed to duplicate the existing function rather than just using it to get output How can I just get "filename" from function2 in function3? – Harry M Feb 26 '19 at 09:46
  • What are you really trying to do? – selbie Feb 26 '19 at 09:46
  • Post the entire code to `read_variable`. I suspect you just need to initialize your `var` struct correctly and then parse the entire file after reading it with a single call to read_variable. – selbie Feb 26 '19 at 09:49
  • I am not allowed to modify read_variable How can I just get "filename" from function2 in function3? – Harry M Feb 26 '19 at 09:53
0

You can't. At least not in a way that's portable and standards compliant. By the time read_variable() returns, f is already out of scope, so it's no longer guaranteed to exist. If you're familiar with the calling stack convention of your target architecture, you may be able to access the now deinitialized memory by doing out of bounds access using pointer arithmetic, and pray that anything useful still remains there, but you really shouldn't do this. Another naughty thing you can do but really shouldn't is if read_variable() didn't close the file and thus have a file handle leak, it may be possible to get access to another file pointer for the same file figuring out the file number and using OS-specific API to access the file. Both of these are doing unsafe access, and invokes undefined behaviours, so you really shouldn't do either of them.

Instead what you can do is open a new file with the same filename, as the filename is returned in var.variable after the call to read_variable(). Either that, or you must modify read_variable() or reimplement read_variable() in a file you can modify.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144