I want to operate on streams using some abstraction and thus I want to use fstream* instead of ifstream and ofstream. I tried to do sth like that but is wil cause access violation:
char* text= "test";
fstream* o = new fstream();
o = &fstream("name.txt");
o->write(text, 4);
o->close();
How can I fix it, or use another idea ?
I want to use pointer in this case (You can look here for more general information) How to implement my own IO file API in C++
After the changes it now looks like this:
class GIO_Persistent_File_System : public GIO_CORE
{
public:
GIO_Persistent_File_System(void);
int open(char*, int);
int close();
void write(char* s, int size);
void read(char* s, int size);
public:
~GIO_Persistent_File_System(void);
private:
fstream file;
};
int GIO_Persistent_File_System::open(char* path, int mode){
file.open(path);
return 0;
}
int GIO_Persistent_File_System::close(){
file.close();
return 0;
}
void GIO_Persistent_File_System::write(char* s, int size){
file.write(s, size);
return;
}
void GIO_Persistent_File_System::read(char* s, int size){
file.read(s, size);
return;
}
MAIN:
GIO_CORE* plik = new GIO_Persistent_File_System();
char* test = new char[10];
char* napis = "testgs";
plik->open("name.txt", OPEN_MODE);
plik->write(napis, 2);
//plik->read(test,2);
plik->close();
And this code seems like working altough I cannot find the file. I checked and the current directory is pointed correctly (ProjectName/Debug)
I checked it and changing fstream to ofstream will works as it should and I can find the file. But since I want to achieve some level of abstraction and I would like to use fstream. How can I fix it ?