0

I have a large C++ codebase that opens various files using the fopenf command (returning a FILE*), and performs operations throughout the code such as writing to and reading from the files using fputc, fgetc, etc. There are also a large amount of formatted print statements (fprintf). My task is to convert the code to use the C++ stream operators instead, creating a stream for each file and using the fmt library for formatted output (fmt::print), as well as the ">>" "<<" operators.

This is a large code base and I would prefer to do an iterative piece-by-piece conversion, i.e., have the new updated code working simultaneously with the old code, and producing exactly the same input/output without creating any extra/new files.

Is there a stop-gap way that I could temporarily get the code to run by writing to the file opened with the fopenf command using the stream operators? My thought would be to get it working that way, and then write a script to go back and replace whatever stop-gap is used with the final code, as well as replacing the open statements, once all the updates have been made.

Or, do I have to convert all of the I/O to use the fstream functionality first?

wand_swe
  • 43
  • 5

1 Answers1

0

Is there a stop-gap way that I could temporarily get the code to run by writing to the file opened with the fopenf command using the stream operators?

That would require you to implement your own (or find a 3rd party) custom std::basic_streambuf class that writes to a pre-existing FILE* pointer. And then, wherever you want to use C++ stream operators on FILE* pointers, you can construct a plain std::ostream object passing it an instance of your custom class for its output buffer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770