Write multiple data in a binary file

closed account (9hU4216C)
Hi, I want to write multiple types of data in a c++ binary file using the .write() method and read it using the .read() method. Here's my code:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {

	const int MAX = 30;
	int length = 0;

	//Input:
	char buffer[MAX];
	char buffer2[MAX];
	int number = 10;
	int number2 = 20;

	//Output:
	char r_buffer[MAX];
	char r_buffer2[MAX];
	int r_number = 0;
	int r_number2 = 0;

	ofstream file;
	file.open("binary.bin", ios_base::out | ios_base::binary);

	if (!file.is_open()) {
		cout << "Something went wrong while creating the file." << endl;
	}
	else {
		strcpy(buffer, "First input in the file.");
		strcpy(buffer2, "Second input in the file.");

		file.write((char*)&buffer, sizeof(buffer));
		file.write((char*)&number, sizeof(int));
		file.write((char*)&buffer2, sizeof(buffer2));
		file.write((char*)&number2, sizeof(int));

		file.close();
	}

	ifstream reader;
	reader.open("binary.bin", ios_base::in | ios_base::binary | ios_base::app);

	if (!reader.is_open()) {
		cout << "Something went wrong while openning the file." << endl;
	}
	else {
		int length = 0;

		reader.seekg(sizeof(buffer), ios_base::beg);
		length = reader.tellg();

		reader.read((char*)r_buffer, length);

		reader.close();

		string sentense = r_buffer;

		cout << sentense << endl; //outputs a blank space
	    
	}

	system("PAUSE");
	return 0;
}


I don't get what I am doing wrong. I am sorry if this topic has already been discused but I looked on many forums and never saw any clear response for my problem.

Thank you!
Last edited on
What's it doing or not doing? Explain the problem. Just by looking at the code, it seems to be correct.
Hi,
Try putting these things in appropriate places :

1.
1
2
3
4
5
6
7
8
9
10
11
12
13
const int MAX = 30;

struct myData
{
     myData() : number(10) {memset(buffer, 0, sizeof(buffer));}

     char buffer[MAX];
     int number;
};

myData data, data2;
myData r_data, r_data2;


2.
1
2
3
4
strcpy(data.buffer, "First input in the file.");
		strcpy(data2.buffer2, "Second input in the file.");
		file.write((char*)&data, sizeof(data));
		file.write((char*)&data2, sizeof(data2));


3.
1
2
3
4
5
6
7
8
9
10
    int length = 0;
	reader.seekg(0, reader.end);
	length = reader.tellg();
	if(length != sizeof(myData) * 2) cout << "Invalid input file size - " << length << endl;

	reader.read((char*)&r_data, sizeof(r_data));
	reader.read((char*)&r_data2, sizeof(r_data2));	reader.close();

	string sentence = r_data.buffer;
		cout << "My meat is " << sentence << endl; // Outputs a blank space (?) 
For your information :
reader.read((char*)r_buffer, length);

This line can potentially cause segfaults.
closed account (48T7M4Gy)
.
Last edited on
Can I ask why I need to cast in (char*) char arrays when I want to read them but not when I want to write them in the file?

The types of r_buffer1 and r_buffer are char * so the casts are not necessary.

BTW, there is an error on line 50. The & should not be there.


Good we were able to help.
closed account (48T7M4Gy)
.
Last edited on
There is no error on line 50

If you remove the unnecessary cast, you indeed see that there is an error.

1>c:\dev\junk\main.cpp(50) : error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'char (*)[80]' to 'char *'


closed account (48T7M4Gy)
.
Last edited on
The claim about removing the & is about as useful as alerting programmers to the effects of removing ; at the end of the line.

You're missing the point(er).

r_buffer2 "decays" to a pointer to the first character of the array. Placing the & in front of r_buffer2 takes the address of that pointer resulting in a pointer to a pointer. The extraneous cast masks the error by telling the compiler to ignore the type mismatch. IOW, read is expecting a char pointer, but is being passed a pointer to a pointer. The read is going to read into memory at the wrong place.
Last edited on
Topic archived. No new replies allowed.