【问题标题】:concatenating string and int for filename in cppcpp中文件名的连接字符串和int
【发布时间】:2015-04-27 19:18:16
【问题描述】:

作为 C++ 的新手,我看过C++ concatenate string and int,但我的要求不太一样。

我有如下示例代码:

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

int main ()
{
std::string name = "John"; int age = 21;
std::string result;
std::ofstream myfile;
char numstr[21]; 
sprintf(numstr, "%d", age);
result = name + numstr;
myfile.open(result);
myfile << "HELLO THERE";
myfile.close();
return 0;
}

字符串和 int 连接通常有效,但在我希望它是文件名时无效。

所以基本上,我希望文件名是字符串和整数的组合。这对我不起作用,我收到错误

'std::string {aka 中的参数 1 没有已知的转换 std::basic_string}' 到 'const char*'

我希望在 for 循环中使用此逻辑

for(i=0;i<100;i++) {
if(i%20==0) {
  result = name + i;
  myfile.open(result);
  myfile << "The value is:" << i;
  myfile.close(); }
}

所以,基本上每 20 次迭代,我需要将这个“值是”打印在一个名为 John20、John40 等的新文件中。所以,对于 100 次迭代,我应该有 5 个文件.

【问题讨论】:

  • 也许可以尝试更多类似 C++ 的方式将 int 转换为 string(std::to_string)
  • 制作一个minimal测试用例。如果您打扰过,您会知道这与连接没有任何关系。您只是没有将正确的参数传递给流构造函数。

标签: c++ string loops concatenation


【解决方案1】:

字符串和 int 连接通常有效,但在我希望它是文件名时无效。

它与连接字符串无关。您的编译器不支持 C++11,这意味着您不能将 std::string 作为参数传递给 std::ofstream::open。您需要一个指向空终止字符串的指针。幸运的是,std::string::c_str() 为您提供:

myfile.open(result.c_str());

注意,你可以直接实例化流:

myfile(result.c_str()); // opens file

至于循环版本,请参阅处理连接整数和字符串的众多副本之一。

【讨论】:

    【解决方案2】:

    您引用的问题与您的字符串连接问题高度相关。如果可能,我建议使用C++11 solution

    #include <fstream>
    #include <sstream>
    
    int main() {
        const std::string name = "John";
        std::ofstream myfile;
        for (int i = 0; i < 100; i += 20) {
            myfile.open(name + std::to_string(i));
            myfile << "The value is:" << i;
            myfile.close();
        }
    }
    

    stringstream solution 兼容:

    #include <fstream>
    #include <sstream>
    
    int main() {
        const std::string name = "John";
        std::ofstream myfile;
        for (int i = 0; i < 100; i += 20) {
            std::stringstream ss;
            ss << name << i;
            myfile.open(ss.str().c_str());
            myfile << "The value is:" << i;
            myfile.close();
        }
    }
    

    此外,您应该:

    • 消除杂散包括&lt;iostream&gt;&lt;stdio.h&gt;
    • 消除using namespace std;,这通常是一种不好的做法——你甚至不需要它。
    • 简化循环
    • 将前缀标记为const

    (您可以使用sprintf(numstr, "%s%d", name.c_str(), i) 编写文件名,但这将是非常糟糕的 C++ 代码。)

    【讨论】:

    • 第一次听说std::to_string
    【解决方案3】:

    如果我们首先查看循环,我们可以选择从 1 而不是 0 开始计数,这样您的第一个文件将是 name+20,并且我们在 i 达到 101 之前停止计数,这样您的最后一个文件是name+100

    我们还需要将.txt 添加到字符串中,以便生成一个文本文件。
    如果您不更改数据(例如名称)。然后我们需要将i转换为字符串,如果你的编译器支持c++11你可以使用std::to_string()。我选择创建一个ostringstream 对象,并存储从成员函数.str() 返回的字符串。

    这是您编辑的循环:

    for(int i=1;i != 101;++i) {
            if(i%20==0) {
                ostringstream temp;            // use a string stream to convert int...
                temp << i;
                str = temp.str();              // ...to str
                result = name + str + ending;  // concatenating strings.
                myfile.open(result);
                myfile << "The value is:" << i;
                myfile.close();
            }
    }
    

    现在最好将此循环放置在一个函数中并将所有相关参数传递给它。这是一个完整的工作demo

    输出文件为:John20.txt、John40.txt、John60.txt、John80.txt、John100.txt
    还有其他方法可以做到这一点,但这应该给你一个大致的想法。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 2019-10-24
      • 1970-01-01
      • 2017-01-05
      • 2013-10-03
      • 1970-01-01
      • 2012-02-24
      • 2011-07-07
      相关资源
      最近更新 更多