Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        12个月前关闭。
                                                                                            
                
        
头文件

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


1.c在某些目录

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

static void filename(const char *variable,


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

int read_variable(some_variable *var)

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


2.c在其他目录

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和function2在某个目录中的一个文件中,function3在我正在编写的另一个文件中。
我在函数3中调用了函数2。
我想在function3中使用来自function2的“文件名”。注意:我无法更改功能2'

最佳答案

你不能

FILE句柄,fread_variable的局部变量。因此,read_variable以外的任何东西都完全无法访问。

您没有向read_variable显示完整的代码,但是考虑到它会将文件句柄打开到局部变量中,所以我希望它也可以在fclose调用之前返回它。

如果不允许您修改read_variable,您是否考虑过将read_variable的整个代码复制到您的own_function调用中并对其进行修改以满足您的需要?

关于c - 如何在C中的另一个函数中使用一个函数中的一个变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54882296/

10-11 22:09
查看更多