将 stdio 流传递给另一个函数 c++

passing stdio stream to another function c++

我有一个函数可以打开并向 stdio FILE* 流写入一些内容。然后我调用另一个函数,该函数将构建一个 xml 然后我想将其写入同一个流。不是将该字符串 xml 传回原始函数,因为它可能变得非常大,我可以将该打开的流传递给 xmlbuilder 函数并继续写入同一个流,以及它离开的地方吗关闭,而在 xmlBuilder 函数中?

当然,像这样:

void writeOtherStuff(FILE* pFile)
{
    fputs("some more data\n", pFile);
}

void myFunction()
{
   FILE* pFile = fopen("myfile.txt", "w");
   if (!pFile)
       return;
   fputs("some data\n", pFile);
   writeOtherStuff(pFile);
   fclose(pFile);
}

是的,你可以。传递 FILE * 指针与传递任何其他类型的指针没有区别。