有没有办法使用流运算符对使用 fopenf 打开的文件执行操作?
我有一个大型的 C++ 代码库,它使用 fopenf 命令(返回 FILE*)打开各种文件,并在整个代码中执行操作,例如使用 fputc、fgetc 等写入和读取文件。还有大量的格式化打印语句(fprintf)。我的任务是将代码转换为使用 C++ 流运算符,为每个文件创建一个流并使用 fmt 库进行格式化输出 (fmt::print) 以及“>>” “<<”运营商。
这是一个很大的代码库,我更愿意进行迭代式的逐段转换,即让新的更新代码与旧代码同时工作,并生成完全相同相同的输入/输出无需创建任何额外/新文件。
有没有一种权宜之计,我可以通过使用流运算符写入使用 fopenf 命令打开的文件来临时运行代码?我的想法是让它以这种方式工作,然后编写一个脚本返回并替换最终代码中使用的任何权宜之计,以及在完成所有更新后替换打开的语句。
或者,我是否必须首先转换所有 I/O 才能使用 fstream 功能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这需要您实现自己的(或找到第三方)自定义
std ::basic_streambuf
类,写入预先存在的FILE*
指针。然后,无论您想在 FILE* 指针上使用 C++ 流运算符,您都可以构造一个普通的std::ostream
对象,为其传递自定义类的实例。输出缓冲区。That would require you to implement your own (or find a 3rd party) custom
std::basic_streambuf
class that writes to a pre-existingFILE*
pointer. And then, wherever you want to use C++ stream operators onFILE*
pointers, you can construct a plainstd::ostream
object passing it an instance of your custom class for its output buffer.