C++ 如何将变量与字符串值组合
我想在(ofstream)文件路径中将变量和字符串值组合在一起 示例:C++ 如何将变量与字符串值组合,c++,C++,我想在(ofstream)文件路径中将变量和字符串值组合在一起 示例: long phNumber; char bufPhNumber[20]; ofstream ifile; cout << "Phone Number: "; cin >> phNumber; itoa(phNumber,bufPhNumber,20); ifile.open("c://" + bufPhNumber + ".txt",ios::out); // error in this lin
long phNumber;
char bufPhNumber[20];
ofstream ifile;
cout << "Phone Number: ";
cin >> phNumber;
itoa(phNumber,bufPhNumber,20);
ifile.open("c://" + bufPhNumber + ".txt",ios::out); // error in this line
长phNumber;
char bufPhNumber[20];
流媒体;
cout>phNumber;
itoa(phNumber,bufPhNumber,20);
ifile.open(“c://”+bufPhNumber+“.txt”,ios::out);//这行有错误
如何将此变量(bufPhNumber)与字符串(“c://“+这里的变量+”.txt”)组合起来执行以下操作:
ifile.open((std::string("c://") + bufPhNumber + ".txt").c_str(),ios::out);
说明:
它首先创建一个字符串,并使用operator+()
将其余的c字符串连接为:
std::string temp = std::string("c://") + bufPhNumber + ".txt";
然后获取c_str()
并将其传递给.open()
:
但是,在C++11中,您不需要执行.C_str()
,并且可以直接使用std::string
更好的解决方案应该是:
std::string phNumber; //declare it as std::string
cout << "Phone Number: ";
cin >> phNumber; //read as string
//use constructor
ofstream ifile(("c://" + phNumber + ".txt").c_str(), ios::out);
std::字符串phNumber//将其声明为std::string
cout>phNumber//读作字符串
//使用构造函数
流ifile(((((“c://“+phNumber+”.txt”).c_str(),ios::out);
执行以下操作:
ifile.open((std::string("c://") + bufPhNumber + ".txt").c_str(),ios::out);
说明:
它首先创建一个字符串,并使用operator+()
将其余的c字符串连接为:
std::string temp = std::string("c://") + bufPhNumber + ".txt";
然后获取c_str()
并将其传递给.open()
:
但是,在C++11中,您不需要执行.C_str()
,并且可以直接使用std::string
更好的解决方案应该是:
std::string phNumber; //declare it as std::string
cout << "Phone Number: ";
cin >> phNumber; //read as string
//use constructor
ofstream ifile(("c://" + phNumber + ".txt").c_str(), ios::out);
std::字符串phNumber//将其声明为std::string
cout>phNumber//读作字符串
//使用构造函数
流ifile(((((“c://“+phNumber+”.txt”).c_str(),ios::out);
至少在C++11(a)之前,Stream::open的要求文件名使用const char*
,而不是std::string
,如所示
而不是:
ifile.open("c://" + bufPhNumber + ".txt",ios::out);
使用以下命令:
string fspec = std::string ("c://") + bufPhNumber + ".txt";
ifile.open (fspec.c_str(), ios::out);
(您可能想考虑为什么您的输出文件被称为“代码> iFILE < /代码>”。< /P>
(a) 在C++11中,有两个open
函数用于basic_of stream
:
void open(const char* s, ios_base::openmode mode = ios_base::out);
void open(const string& s, ios_base::openmode mode = ios_base::out);
因此,一个字符串
版本将在那里工作。of stream::open
,至少在C++11(a)之前,要求文件名使用常量char*
,而不是std::string
,如所示
而不是:
ifile.open("c://" + bufPhNumber + ".txt",ios::out);
使用以下命令:
string fspec = std::string ("c://") + bufPhNumber + ".txt";
ifile.open (fspec.c_str(), ios::out);
(您可能想考虑为什么您的输出文件被称为“代码> iFILE < /代码>”。< /P>
(a) 在C++11中,有两个open
函数用于basic_of stream
:
void open(const char* s, ios_base::openmode mode = ios_base::out);
void open(const string& s, ios_base::openmode mode = ios_base::out);
所以一个字符串
版本可以在那里工作。好的,大家好。您不能像在java中那样直接连接字符串,因此您的问题是:
bufPhNumber+“.txt”
假设bufPhNumber是char*,text是char*,它们不支持您想要的+运算符
这样的作业有strcat(),但它假定目标字符串有足够的空间容纳目标字符串,并且它还修改目标字符串
char Array[28]; //One for the null
strcat(Array,"C:\\");
strcat(Array,bufPhNumber);
strcat(Array,".txt");
虽然我推荐使用一个字符数组来表示电话号码,但它不太适合这样的存储,因为它不能容纳你喜欢的数字(你可以考虑使用两个int / long)。此外,考虑使用无符号长(因为你没有得到负的电话号码)。如果使用long/int,请注意从long到char数组以及从char数组到long数组的转换将占用大量内存和处理能力,这在更大的范围内比仅使用char数组的效率要低。好的,您好。您不能像在java中那样直接连接字符串,因此您的问题是:
bufPhNumber+“.txt”
假设bufPhNumber是char*,text是char*,它们不支持您想要的+运算符
这样的作业有strcat(),但它假定目标字符串有足够的空间容纳目标字符串,并且它还修改目标字符串
char Array[28]; //One for the null
strcat(Array,"C:\\");
strcat(Array,bufPhNumber);
strcat(Array,".txt");
虽然我推荐使用一个字符数组来表示电话号码,但它不太适合这样的存储,因为它不能容纳你喜欢的数字(你可以考虑使用两个int / long)。此外,考虑使用无符号长(因为你没有得到负的电话号码)。如果确实使用long/int,请注意,从long到char数组以及从char数组到long数组的转换将占用大量内存和处理能力,这在更大的范围内比仅使用char数组的效率要低。更好的解决方案是在流的中输入错误。它仍然是bufPHNumber
@Nawaz:谢谢你的回答,但这不是我想要的,我想使用int-value并转换成char,然后在文件路径中使用它,我不想使用字符串类型而不是int-type或long@LionKing无论如何,使用整数类型作为电话号码是个坏主意。如何引导0
s或+
s或``分隔符或-
分隔符。电话号码实际上只是一个标识符,为了方便起见,这些符号是十进制数字,而不是来自其他奇怪的字母表。更好的解决方案是在stream
ctor的中输入错误。它仍然是bufPHNumber
@Nawaz:谢谢你的回答,但这不是我想要的,我想使用int-value并转换成char,然后在文件路径中使用它,我不想使用字符串类型而不是int-type或long@LionKing无论如何,使用整数类型作为电话号码是个坏主意。如何引导0
s或+
s或``分隔符或-
分隔符。电话号码实际上只是一个标识符,为了方便起见,这些符号是十进制数字,而不是其他奇怪的字母表。@rubenvb:说得好。我更新了答案以涵盖这一点。谢谢。paxdiablo:一个提供如此多有用便利的新标准不应该不被宣传:)@rubenvb:good point。我更新了答案以涵盖这一点。paxdiablo:一个提供了如此多有用便利的新标准不应该不被宣传:)错误的答案。这是一个关于C++而不是C的问题,你可以完美地连接<代码> STD::STR