I have a function that opens and writes some stuff to a stdio FILE* stream. I then call another function that will build an xml that I want to then write to that same stream. Rather than passing that string xml back to the original function, because it may get very big, can I pass that open stream to the xmlbuilder function and continue writing to the same stream, and where it left off, while in the xmlBuilder function?
Asked
Active
Viewed 72 times
-4
-
6One word answer: Yes. – user253751 Apr 07 '16 at 21:06
-
Terrible mistmatch of tag (c++) and mechanism - C-style `FILE*` – SergeyA Apr 07 '16 at 21:16
-
hmmm, that's odd, I had 'c-style' tag there along with 'stream' but only three of them showed up. – Saif Ahsanullah Apr 07 '16 at 21:17
2 Answers
3
Sure, something like this:
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);
}

Dan Korn
- 1,274
- 9
- 14
2
Yes you can. Passing a FILE *
pointer is no different from passing any other type of pointer.

jotik
- 17,044
- 13
- 58
- 123