Hello! I'm having some issues with binary file I/O. I've never used it before and I'm having some trouble. I've got a cpp file with my main function, then I've got a class I've made tsuPod.cpp and the header for it tsuPod.h
For some reason, my binary input/output file is not being created or written to, and I'm not sure why. I listed the fstream as a private member of the tsuPod class and I'm opening the file in the constructor when it is called from main. I think this is the problem, but I'm not sure why. Thank you in advance for help!
Of course I appreciate the help, but if I coded something, it was because I thought that it was right. Restating something I coded with no explanation doesn't exactly help, but I appreciate you pointing out where you think the error could have occurred.
I thought the int parameter of the write() method was the location of where to start writing in the file. I chose 0, for the beginning of the file. For grins I also tried 100 and still no luck.
That was very helpful thank you! However, I've tracked the problem down even further. Now that I've got a proper function call the file is STILL not being created. Perhaps I'm going crazy?
1 2 3 4 5 6
fstream mem1;
mem1.open("tsupod_memory.dat", ios::in | ios::out | ios::binary);
if (mem1)
cout << "open!";
char test = 'o';
mem1.write(&test, sizeof(test));
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
//Uncomment the section below to see the difference
fstream file("data.dat", /*ios_base::in | */ios_base::out | ios_base::binary);
if(!file.is_open())
cout << "Unable to open.";
else{
//Reopen because the standard I/O library does not allow you to
// change the openmode without opening a file
file.close();
file.open("data.dat", ios_base::in | ios_base::out | ios_base::binary);
string s("Hello world!");
file.write(s.c_str(), s.size());
char buf[100] = {'\0'};
file.seekg(0);
file.read(buf, 100);
cout << buf;
}
return 0;
}