在cpp中为文件名连接字符串和int
concatenating string and int for filename in cpp
作为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;
}
字符串和整数连接通常有效,但当我希望它成为文件名时却无效。
基本上,我希望文件名是字符串和整数的组合。这对我不起作用,我收到错误
no known conversion for argument 1 from ‘std::string {aka
std::basic_string}’ to ‘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 次迭代,我需要将此 "The value is" 打印在一个新文件中,该文件的名称将是 John20、John40 等等。因此,对于 100 次迭代,我应该5 个文件。
The string and int concatenation works generally but not when I want it to be a filename.
它与连接字符串无关。您的编译器不支持 C++11,这意味着您不能将 std::string
作为参数传递给 std::ofstream::open
。您需要一个指向空终止字符串的指针。幸运的是,std::string::c_str()
给你:
myfile.open(result.c_str());
注意可以直接实例化流:
myfile(result.c_str()); // opens file
至于循环版本,请参阅处理连接整数和字符串的众多副本之一。
您引用的问题与您的字符串连接问题高度相关。如果可能,我建议使用 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();
}
}
此外,您应该:
- 消除杂散包括
<iostream>
和<stdio.h>
- 消除
using namespace std;
,这通常是不好的做法 — 您甚至不需要它。
- 简化循环
- 将前缀标记为
const
(您 可以 使用 sprintf(numstr, "%s%d", name.c_str(), i)
编写文件名,但那将是非常糟糕的 C++ 代码。)
如果我们首先从查看循环开始,我们可以选择从 1 而不是 0 开始计数,这样您的第一个文件将是 name+20
,我们在 i
之前停止计数点击 101,这样你的最后一个文件将是 name+100
。
我们还需要将 .txt
添加到字符串中,以便制作文本文件。
您可以将这些参数作为引用或引用传递给函数const 如果您不更改数据(例如名称)。然后我们需要将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
还有其他方法可以做到这一点,但这应该给你一个大概的想法。希望对你有帮助。
作为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;
}
字符串和整数连接通常有效,但当我希望它成为文件名时却无效。
基本上,我希望文件名是字符串和整数的组合。这对我不起作用,我收到错误
no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘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 次迭代,我需要将此 "The value is" 打印在一个新文件中,该文件的名称将是 John20、John40 等等。因此,对于 100 次迭代,我应该5 个文件。
The string and int concatenation works generally but not when I want it to be a filename.
它与连接字符串无关。您的编译器不支持 C++11,这意味着您不能将 std::string
作为参数传递给 std::ofstream::open
。您需要一个指向空终止字符串的指针。幸运的是,std::string::c_str()
给你:
myfile.open(result.c_str());
注意可以直接实例化流:
myfile(result.c_str()); // opens file
至于循环版本,请参阅处理连接整数和字符串的众多副本之一。
您引用的问题与您的字符串连接问题高度相关。如果可能,我建议使用 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();
}
}
此外,您应该:
- 消除杂散包括
<iostream>
和<stdio.h>
- 消除
using namespace std;
,这通常是不好的做法 — 您甚至不需要它。 - 简化循环
- 将前缀标记为
const
(您 可以 使用 sprintf(numstr, "%s%d", name.c_str(), i)
编写文件名,但那将是非常糟糕的 C++ 代码。)
如果我们首先从查看循环开始,我们可以选择从 1 而不是 0 开始计数,这样您的第一个文件将是 name+20
,我们在 i
之前停止计数点击 101,这样你的最后一个文件将是 name+100
。
我们还需要将 .txt
添加到字符串中,以便制作文本文件。
您可以将这些参数作为引用或引用传递给函数const 如果您不更改数据(例如名称)。然后我们需要将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
还有其他方法可以做到这一点,但这应该给你一个大概的想法。希望对你有帮助。