-4

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?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Saif Ahsanullah
  • 204
  • 3
  • 13

2 Answers2

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