C++:如何将 fprintf 结果作为 std::string 不带 sprintf [英] C++: how to get fprintf results as a std::string w/o sprintf

查看:23
本文介绍了C++:如何将 fprintf 结果作为 std::string 不带 sprintf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个用 C++ 实现的开源 UNIX 工具,我需要更改一些代码来让它做我想做的事情.我想做最小的改变,希望我的补丁在上游被接受.可在标准 C++ 中实现且不会创建更多外部依赖项的解决方案是首选.

I am working with an open-source UNIX tool that is implemented in C++, and I need to change some code to get it to do what I want. I would like to make the smallest possible change in hopes of getting my patch accepted upstream. Solutions that are implementable in standard C++ and do not create more external dependencies are preferred.

这是我的问题.我有一个 C++ 类——我们称之为A"——它目前使用 fprintf() 将其高度格式化的数据结构打印到文件指针.在它的打印函数中,它还递归地调用了几个成员类的相同定义的打印函数(B"就是一个例子).还有另一个类 C 有一个成员 std::string "foo",需要将其设置为 A 实例的 print() 结果.将其视为 A 的 to_str() 成员函数.

Here is my problem. I have a C++ class -- let's call it "A" -- that currently uses fprintf() to print its heavily formatted data structures to a file pointer. In its print function, it also recursively calls the identically defined print functions of several member classes ("B" is an example). There is another class C that has a member std::string "foo" that needs to be set to the print() results of an instance of A. Think of it as a to_str() member function for A.

在伪代码中:

class A {
public:
  ...

  void print(FILE* f);
  B b;

  ...  
};

...

void A::print(FILE *f)
{
  std::string s = "stuff";
  fprintf(f, "some %s", s);
  b.print(f);
}

class C {
  ...
  std::string foo;
  bool set_foo(std::str);
  ...
}

...

A a = new A();
C c = new C();

...

// wish i knew how to write A's to_str()
c.set_foo(a.to_str());

我应该提到 C 相当稳定,但 A 和 B(以及 A 的其他依赖项)处于不断变化的状态,因此所需的代码更改越少越好.当前的 print(FILE* F) 接口也需要保留.我考虑了几种实现 A::to_str() 的方法,每种方法都有优点和缺点:

I should mention that C is fairly stable, but A and B (and the rest of A's dependents) are in a state of flux, so the less code changes necessary the better. The current print(FILE* F) interface also needs to be preserved. I have considered several approaches to implementing A::to_str(), each with advantages and disadvantages:

  1. 将对 fprintf() 的调用更改为 sprintf()

  1. Change the calls to fprintf() to sprintf()

  • 我不必重写任何格式字符串
  • print() 可以重新实现为:fprint(f, this.to_str());
  • 但是我需要手动分配char[]s,合并很多c字符串,最后将字符数组转换为std::string

尝试在字符串流中捕获 a.print() 的结果

Try to catch the results of a.print() in a string stream

  • 我必须将所有格式字符串转换为 <<输出格式.有数百个 fprintf() 可以转换:-{
  • print() 必须重写,因为我知道没有标准方法可以从 UNIX 文件句柄创建输出流(尽管 这家伙说有可能).

使用Boost的字符串格式库

Use Boost's string format library

  • 更多的外部依赖.糟糕.
  • Format 的语法与 printf() 的语法大相径庭:

printf(format_str, args) -> cout <<boost::format(format_str) % arg1 % arg2 % 等

printf(format_str, args) -> cout << boost::format(format_str) % arg1 % arg2 % etc

使用Qt的QString::asprintf()

  • 不同的外部依赖项.

那么,我是否已经用尽了所有可能的选择?如果是这样,你认为哪个是我最好的选择?如果没有,我忽略了什么?

So, have I exhausted all possible options? If so, which do you think is my best bet? If not, what have I overlooked?

谢谢.

推荐答案

我正在使用#3:boost 字符串格式库 - 但我不得不承认我从来没有遇到过格式规范差异的问题.

I am using #3: the boost string format library - but I have to admit that I've never had any problem with the differences in format specifications.

对我来说就像一个魅力 - 外部依赖可能会更糟(一个非常稳定的库)

Works like a charm for me - and the external dependencies could be worse (a very stable library)

已添加一个如何使用 boost::format 代替 printf 的示例:

Edited: adding an example how to use boost::format instead of printf:

sprintf(buffer, "This is a string with some %s and %d numbers", "strings", 42);

在 boost::format 库中会是这样的:

would be something like this with the boost::format library:

string = boost::str(boost::format("This is a string with some %s and %d numbers") %"strings" %42);

希望这有助于澄清 boost::format 的用法

Hope this helps clarify the usage of boost::format

我在 4 或 5 个应用程序中使用 boost::format 作为 sprintf/printf 替换(将格式化的字符串写入文件,或自定义输出到日志文件)并且从未遇到格式差异问题.可能有一些(或多或少晦涩)不同的格式说明符 - 但我从来没有遇到过问题.

I've used boost::format as a sprintf / printf replacement in 4 or 5 applications (writing formatted strings to files, or custom output to logfiles) and never had problems with format differences. There may be some (more or less obscure) format specifiers which are differently - but I never had a problem.

相比之下,我有一些格式规范,我无法真正使用流(据我记得)

In contrast I had some format specifications I couldn't really do with streams (as much as I remember)

这篇关于C++:如何将 fprintf 结果作为 std::string 不带 sprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆