gpt4 book ai didi

在 C 函数中更改文件指针指向的值

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:36 24 4
gpt4 key购买 nike

是否可以通过引用传递来更改 C 函数中 FILE 指针指向的值?

这是一个示例来尝试说明我正在尝试做的事情,我可以修改整数但不能修改文件指针,有什么想法吗?

int main (void)
{
FILE* stream;
int a = 1;
configure_stream(rand() % 100, &a, &stream);
return 0;
}

void configure_stream(int value, int* modifyMe, FILE* stream)
{
*modifyMe = rand() % 100;
if (value > 50)
{
*stream = stderr;
}
else
{
*stream = stdout;
}
}

最佳答案

我想你想声明你的 configure_stream 如下

void configure_stream(int value, int* modifyMe, FILE** stream)
{
*modifyMe = rand() % 100;
if (value > 50)
{
*stream = stderr;
}
else
{
*stream = stdout;
}
}

并与它一起使用

configure_stream(rand() % 100, &a, &stream)

这会得到一个指向 FILE 指针的指针。使用指向指针的指针,您可以修改 FILE 指针,而不会在内存的大丛林中丢失它

关于在 C 函数中更改文件指针指向的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6965439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com