Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用变量命名文件_C++_File_Filenames - Fatal编程技术网

C++ C++;使用变量命名文件

C++ C++;使用变量命名文件,c++,file,filenames,C++,File,Filenames,基本上,我希望能够使用程序中定义的两个变量(程序员未知的值)创建一个文件名。我可以只使用一个变量(即(username+“.txt”),但由于某些原因,使用两个变量会把事情搞砸 这是我的密码 void User::setTicket(std::string username, int i) { std::ofstream fout (username + "Ticket" + i + ".txt"); // Some code fout.close(); }

基本上,我希望能够使用程序中定义的两个变量(程序员未知的值)创建一个文件名。我可以只使用一个变量(即(username+“.txt”),但由于某些原因,使用两个变量会把事情搞砸

这是我的密码

void User::setTicket(std::string username, int i)
{
    std::ofstream fout (username + "Ticket" + i + ".txt");

    // Some code

        fout.close();
}
int i本质上是在main中的循环中初始化的计数数字,因此每次循环循环都会调用setTicket,希望生成的文件会被调用

user1Ticket1.txt
user1Ticket2.txt
user1Ticket3.txt

etc

为了使用字符串的运算符+,必须使用
std::to_string(i)
(c++11)将整数转换为字符串


另请参见:

为了使用字符串的运算符+,必须使用
std::to_string(i)
(c++11)将整数转换为字符串


另请参见:

基本上,有三种方法可以做到这一点

如果您有一个c++11编译器,您可以使用std::to_字符串(i),正如@Dlotan所指出的:

std::ofstream fout(username + "Ticket" + std::to_string(i) + ".txt");
如果您想使用boost:

std::ofstream fout(username + "Ticket" + boost::lexical_cast<std::string>(i) + ".txt");
stringstream ss;
ss << i;
std::ofstream fout(username + "Ticket" + ss.str() + ".txt");
std::ofstreamfout(用户名+“票证”+boost::词法转换(i)+“.txt”);
如果您没有c++11编译器,也不想使用boost:

std::ofstream fout(username + "Ticket" + boost::lexical_cast<std::string>(i) + ".txt");
stringstream ss;
ss << i;
std::ofstream fout(username + "Ticket" + ss.str() + ".txt");
stringstreamss;

ss基本上有三种方法

如果您有一个c++11编译器,您可以使用std::to_字符串(i),正如@Dlotan所指出的:

std::ofstream fout(username + "Ticket" + std::to_string(i) + ".txt");
如果您想使用boost:

std::ofstream fout(username + "Ticket" + boost::lexical_cast<std::string>(i) + ".txt");
stringstream ss;
ss << i;
std::ofstream fout(username + "Ticket" + ss.str() + ".txt");
std::ofstreamfout(用户名+“票证”+boost::词法转换(i)+“.txt”);
如果您没有c++11编译器,也不想使用boost:

std::ofstream fout(username + "Ticket" + boost::lexical_cast<std::string>(i) + ".txt");
stringstream ss;
ss << i;
std::ofstream fout(username + "Ticket" + ss.str() + ".txt");
stringstreamss;

ss数字类型不能隐式转换为字符串或直接附加到字符串

在C++11或更高版本中,有一个库函数用于转换它们:

std::ofstream fout (username + "Ticket" + std::to_string(i) + ".txt");
历史上,字符串流可以从任意类型生成字符串:

std::ostringstream ss;
ss << username << "Ticket" << i << ".txt";
std::ofstream fout (ss.str().c_str());
std::ostringstream ss;

ss数字类型不能隐式转换为字符串或直接附加到字符串

在C++11或更高版本中,有一个库函数用于转换它们:

std::ofstream fout (username + "Ticket" + std::to_string(i) + ".txt");
历史上,字符串流可以从任意类型生成字符串:

std::ostringstream ss;
ss << username << "Ticket" << i << ".txt";
std::ofstream fout (ss.str().c_str());
std::ostringstream ss;

你读过吗?你读过了吗?第二个版本的工作很出色,我想这意味着Visual Studio 2010使用了一个旧版本的C++辉煌,第二个版本已经工作了,我想这意味着Visual Studio 2010使用了一个较老的C++版本。