C++ 在c+中打开文件+;使用变量
需要使用变量和字符串组合使用infle.open()打开文件的帮助C++ 在c+中打开文件+;使用变量,c++,file,file-io,C++,File,File Io,需要使用变量和字符串组合使用infle.open()打开文件的帮助 ifstream infile; infile.open("data_x.txt"); 我需要类似的东西,但是DATAXX.TXT中的X必须是可以改变的变量。< P> C++,可以使用字符串流< /P> std::ostringstream file_name; file_name << "data_" << somevar; infile.open(file_name.str()); std::o
ifstream infile;
infile.open("data_x.txt");
我需要类似的东西,但是DATAXX.TXT中的X必须是可以改变的变量。< P> C++,可以使用字符串流< /P>
std::ostringstream file_name;
file_name << "data_" << somevar;
infile.open(file_name.str());
std::ostringstream文件名;
FielyNox C++,可以使用字符串流
std::ostringstream file_name;
file_name << "data_" << somevar;
infile.open(file_name.str());
std::ostringstream文件名;
FielyNox C++,可以使用字符串流
std::ostringstream file_name;
file_name << "data_" << somevar;
infile.open(file_name.str());
std::ostringstream文件名;
FielyNox C++,可以使用字符串流
std::ostringstream file_name;
file_name << "data_" << somevar;
infile.open(file_name.str());
std::ostringstream文件名;
我不知道我是否正确理解了你的问题。您是否需要打开一个名为“something_X.txt”的文件,但可以用某个变量的值替换“X”?如果是:
template <typename T>
ifstream read_file(const char* baseName, const T& value, const char* extension)
{
std::ostringstream file_path; // Here we will assemble our name
file_path<<baseName<<value<<extension; // Add baseName, value and extension.
ifstream file_handle(file_path.str()); // Open the file.
if(!file_handle)
{
//This file doesn't exist! Notify user in some way.
}
return file_handle; // Return handle to file we have opened.
}
我提供了一个更通用的解决方案,所以您可以自定义名称的常量前缀(“data_”,在您的特定情况下)和文件扩展名。示例:此循环将打开“texture_1.jpg”、“texture_2.jpg”、“texture_10.jpg”
const char*base\u name=“texture”;
常量字符*扩展=“.png”;
对于(int i=1;i我不知道我是否正确理解了您的问题。您是否需要打开一个名为“something_X.txt”的文件,但可以用某个变量的值替换“X”?如果需要:
template <typename T>
ifstream read_file(const char* baseName, const T& value, const char* extension)
{
std::ostringstream file_path; // Here we will assemble our name
file_path<<baseName<<value<<extension; // Add baseName, value and extension.
ifstream file_handle(file_path.str()); // Open the file.
if(!file_handle)
{
//This file doesn't exist! Notify user in some way.
}
return file_handle; // Return handle to file we have opened.
}
我提供了一个更通用的解决方案,所以您可以自定义名称的常量前缀(“数据”在您的特定情况下)和文件扩展名
const char*base\u name=“texture”;
常量字符*扩展=“.png”;
对于(int i=1;i我不知道我是否正确理解了您的问题。您是否需要打开一个名为“something_X.txt”的文件,但可以用某个变量的值替换“X”?如果需要:
template <typename T>
ifstream read_file(const char* baseName, const T& value, const char* extension)
{
std::ostringstream file_path; // Here we will assemble our name
file_path<<baseName<<value<<extension; // Add baseName, value and extension.
ifstream file_handle(file_path.str()); // Open the file.
if(!file_handle)
{
//This file doesn't exist! Notify user in some way.
}
return file_handle; // Return handle to file we have opened.
}
我提供了一个更通用的解决方案,所以您可以自定义名称的常量前缀(“数据”在您的特定情况下)和文件扩展名
const char*base\u name=“texture”;
常量字符*扩展=“.png”;
对于(int i=1;i我不知道我是否正确理解了您的问题。您是否需要打开一个名为“something_X.txt”的文件,但可以用某个变量的值替换“X”?如果需要:
template <typename T>
ifstream read_file(const char* baseName, const T& value, const char* extension)
{
std::ostringstream file_path; // Here we will assemble our name
file_path<<baseName<<value<<extension; // Add baseName, value and extension.
ifstream file_handle(file_path.str()); // Open the file.
if(!file_handle)
{
//This file doesn't exist! Notify user in some way.
}
return file_handle; // Return handle to file we have opened.
}
我提供了一个更通用的解决方案,所以您可以自定义名称的常量前缀(“数据”在您的特定情况下)和文件扩展名
const char*base\u name=“texture”;
常量字符*扩展=“.png”;
对于(inti=1;i您是第二个用法错误的示例)它打开“personalinfo.ini”,函数没有添加下划线…“You's Second example of usage is error”->它不是。这是完全有效的代码,如果存在,将打开文件。如果有任何错误,则是注释。这是我们通常称之为“typo error”的内容。修复了。我的错误,注释中有一个打字错误。8-“You's Second example of usage is error”它将打开“personalinfo.ini”,函数没有添加下划线…“You's Second example of usage is error”->它不是。这是完全有效的代码,如果存在,将打开文件。如果有任何错误,则是注释。这是我们通常称为“typo error”的内容。修复了。我的错误,注释中有一个输入错误。8-)你是第二个用法错误的例子,它会打开“personalinfo.ini”“,函数没有添加下划线…“You's Second example of usage is error”->不是。这是完全有效的代码,如果存在,将打开文件。如果有任何错误,则是注释。这是我们通常称之为“typo error”的内容。修复了。我的错误,注释中有一个输入错误。8-“You's Second example of usage is error”将打开它“personalinfo.ini”,函数没有添加下划线…“You's Second example of usage is error”->它不是。这是完全有效的代码,如果存在,将打开文件。如果有任何错误,则是注释。这是我们通常称为“typo error”的内容。修复了。我的错误,注释中有一个输入错误。8-)