Binary File I/O Issues

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!

Call from main:
 
tsuPod tpod(memory, capacity);


from tsuPod.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class tsuPod
{
	private:
		int max_memory;
		int song_capacity;
		int remaining_memory;
		int total_songs;
		fstream memory;
	public:
		tsuPod(int, int);
		void showSongList();
		int addSong ();
		int removeSong ();
		void clearMemory();
		void shuffle ();
		void sortSongList();
		void statusCheck (int);
		int getTotalMemory() const
			{ return max_memory; }
		int getRemainingMemory() const
			{ return remaining_memory; }
};


from tsuPod.cpp
1
2
3
4
5
6
7
8
9
10
11
12
tsuPod::tsuPod (int max_mem, int song_cap)
{
	max_memory = max_mem;
	remaining_memory = max_mem;
	song_capacity = song_cap;
	total_songs = 0;
	memory.open("tsupod_memory.dat", ios::in | ios::out | ios::binary);

	memory.write("Howdy", 0);

	return;
}
memory.write("Howdy", 0 );
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.

Any more ideas?
You could look at the documentation for the write() function. Understanding what each parameter is used for, and how the function is intended to work will be useful.
http://www.cplusplus.com/reference/ostream/ostream/write/
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));


I'm not even getting the cout << "open!";
It is because of your open mode. It includes ios::in, so the program will not create the file if it does not yet exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <fstream>

using namespace 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;
}
Last edited on
Suddenly, it all makes sense. Thank you!

So I should be able to open it for output only to create it, then close it, then open it for input AND output.

About to go test, thank you again!
Worked, as expected. Thank you again.
Topic archived. No new replies allowed.