Ok. I want to make a generic file reader that I can use for a number of projects this semester. I figured i'd use templates to make the job easier but for some reason I cannot get it to work. I'm not sure what's wrong. What I have isn't working for some reason. Unfortunately I have little experience working with bin files so that may be my issue....
Here is my basic idea in a nutshell - what's wrong?
CODE
#include <fstream>
#include <string>
using namespace std;
template <class T>
class FileIO
{
private:
fstream file;
public:
FileIO();
void Write(T data);
};
template <class T>
FileIO<T>::FileIO()
{
file.open("brandon.bin", ios::out | ios::binary);
}
template <class T>
void FileIO<T>::Write(T data)
{
file.write(static_cast<char*>(data), sizeof(data));
}
int main()
{
string name;
FileIO<string> myfile;
myfile.Write(name);
return 0;
}
This is assuming a lot as well. I know im missing my .close() and other things....
Am I using the template and/or &fstream write() function right?
Well scratch that. I realized my mistake with the fstream write() function. Is everything else ok?