【问题标题】:Iterative procedure naming迭代过程命名
【发布时间】:2015-06-04 22:40:50
【问题描述】:

我有一个进程在一张图片上运行多次。我想使用递增的名称保存每个中间图片结果;

即iteration1.png、iteration2.png等。每次执行之间迭代次数可能会发生变化。

我在创建名称时遇到问题。

我使用 char 字符串而不是字符串作为名称(就是这样,我收到了像这样使用它的函数)。

我将如何编码 name = "iteration"+iter+".p​​ng"?

我试过 strcat,我试过加法(+)。

我的最后一次尝试: char name[] = strcat("result/",(char)i); name = strcat(name,".png");

谢谢

【问题讨论】:

  • 我们需要更多代码,否则您会遇到错误
  • 尝试在文件名中使用当前时间
  • 我添加了我的最后一次尝试。

标签: c++ string loops char


【解决方案1】:

你可以试试

const int MAX_LEN = 20;
char name[MAX_LEN + 1];
snprintf(name, MAX_LEN, "iteration%d.png", iter).

【讨论】:

  • 如果使用 C lib 函数,可能更喜欢 snprintf 而不是 sprintf,至少现在是这样。
  • 通过使用%03d,您甚至可以拥有很棒的名字,例如:iteration009。这在对文件进行排序时可能很有用。
【解决方案2】:

我会写这样的:

std::ostringstream o;

o << "iteration" << iter << ".png";

,使用 o.str() 作为文件名。

【讨论】:

  • 这给出了正确的输出。但是我的输出命令不接受它。 cvSaveImage(o.str(), image2); 给我错误 Invalid arguments ' Candidates are: int cvSaveImage(const char *, const void *, const int *) '
  • o.str().c_str() 如果想转换为 C 风格的字符串应该没问题。只是不要在 cvSaveImage 中存储结果指针或对其进行任何恶意操作。还要记住每次迭代都创建“o”或调用 o.clear() 否则它会累积。
  • 这也是 _ - 不能将参数 '1' 的 'std::basic_ostringstream::__string_type {aka std::basic_string}' 转换为 'const char*' int cvSaveImage(const char*, const CvArr*, const int*)'_
  • 我的小仓库工作,“cvSaveImage(o.str().c_str(), ...”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 2021-07-03
  • 2020-05-13
相关资源
最近更新 更多