How can I use the same function template to save 3 different arrays to 3 different files? (one array per file)
如何使用相同的功能模板将3个不同的数组保存到3个不同的文件中? (每个文件一个数组)
The array types are int
, float
, and char
数组类型是int,float和char
The closest I've gotten is the following code:
我得到的最接近的是以下代码:
template <typename T>
void saveToTextFile(T *arr, const int size)
{
ofstream outFile("arraytextfile.txt", ios::out);`
for(unsigned int i = 0; i < size; ++i)
{
outFile << arr[i] << ' ';
}// end for
outFile << endl;
outFile.close();
}
This, of course, creates a new text file the first time it is called, then truncates the file when opened again. I need to call this template 3 different times and have it save data to a new file each time. Each file should contain a different array.
当然,这会在第一次调用时创建一个新的文本文件,然后在再次打开时截断该文件。我需要将此模板调用3次,并且每次都将数据保存到新文件中。每个文件应包含不同的数组。
2 个解决方案
#1
0
If you're sure that you're calling this function with only three different types, and you're using at least C++11, a simple (but not really elegant) solution is use type traits and check the type; something like
如果您确定只使用三种不同类型调用此函数,并且至少使用C ++ 11,那么简单(但不是非常优雅)的解决方案是使用类型特征并检查类型;就像是
std::ofstream outFile(
std::is_same<T, char>::value
? "file-char.txt"
: (std::is_same<T, int>::value
? "file-int.txt"
: "file-double.txt"), std::ios::out);
If you cant use C++11 or newer, you can simulate std::is_same
with something as follows
如果你不能使用C ++ 11或更新版本,你可以使用以下内容模拟std :: is_same
template <typename, typename>
struct isSame
{ static const bool value = false; };
template <typename T>
struct isSame<T, T>
{ static const bool value = true; };
But for a more generic solution, you can use typeid()
; by example
但是对于更通用的解决方案,您可以使用typeid();通过例子
std::string fName = "file-";
fName += typeid(*arr).name();
fName += ".txt";
std::ofstream outFile(fName, std::ios::out);
but take in count that the typeid()
give a name()
that is implementation dependent.
但请记住,typeid()给出了一个依赖于实现的名称()。
#2
0
Why don't you pass the filePath
via parameter, using a pointer
为什么不使用指针通过参数传递filePath
void saveToTextFile(T *arr, const int size, const char *outputFilePath) {
ofstream outFile(outputFilePath , ios::out);`
}
you'd pass the filePath
to your function like:
你将filePath传递给你的函数,如:
const char * location = "a/c/d.text".c_str();
saveToTextFile(arr, 5, location) // or for short
saveToTextFile(arr, 5,"a/c/d.text".c_str())
#1
0
If you're sure that you're calling this function with only three different types, and you're using at least C++11, a simple (but not really elegant) solution is use type traits and check the type; something like
如果您确定只使用三种不同类型调用此函数,并且至少使用C ++ 11,那么简单(但不是非常优雅)的解决方案是使用类型特征并检查类型;就像是
std::ofstream outFile(
std::is_same<T, char>::value
? "file-char.txt"
: (std::is_same<T, int>::value
? "file-int.txt"
: "file-double.txt"), std::ios::out);
If you cant use C++11 or newer, you can simulate std::is_same
with something as follows
如果你不能使用C ++ 11或更新版本,你可以使用以下内容模拟std :: is_same
template <typename, typename>
struct isSame
{ static const bool value = false; };
template <typename T>
struct isSame<T, T>
{ static const bool value = true; };
But for a more generic solution, you can use typeid()
; by example
但是对于更通用的解决方案,您可以使用typeid();通过例子
std::string fName = "file-";
fName += typeid(*arr).name();
fName += ".txt";
std::ofstream outFile(fName, std::ios::out);
but take in count that the typeid()
give a name()
that is implementation dependent.
但请记住,typeid()给出了一个依赖于实现的名称()。
#2
0
Why don't you pass the filePath
via parameter, using a pointer
为什么不使用指针通过参数传递filePath
void saveToTextFile(T *arr, const int size, const char *outputFilePath) {
ofstream outFile(outputFilePath , ios::out);`
}
you'd pass the filePath
to your function like:
你将filePath传递给你的函数,如:
const char * location = "a/c/d.text".c_str();
saveToTextFile(arr, 5, location) // or for short
saveToTextFile(arr, 5,"a/c/d.text".c_str())