Can't read a binary file.

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
/*
 * main.cpp
 *
 *  Created on: Oct 31, 2012
 *      Author: unkn0wn
 */
#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;
fstream data("data.dat",ios::in|ios::out|ios::binary);

class ABC {
public:
	string a;
	int i;
	ABC(){a.clear(); i=0;}

};

void read()
{
	int i=0;
		do
		{
			ABC A;
			++i;
			cout<<"Enter your NAME : ";
			cin>>A.a;
			A.i=i;
			cout<<A.a<<" your id is "<<A.i<<endl;
			data.write(reinterpret_cast<const char *>(&A),sizeof(ABC));
		}while(i<10);
}

void show()
{
	while(data&&!data.eof())
	{
	ABC A;
	data.read(reinterpret_cast<char *>(&A),sizeof(ABC));
	cout<<A.a<<"\t"<<A.i<<endl;
	}
}

int main()
{
        read();
	data.clear();
	data.seekg(0,ios::beg);
	show();
}

I wish to read and write through files in my code. SO that my data would be saved in the case of new run of executable.

But i am not able to run above code.I don't know where i am wrong. Can anyone help me?.
Last edited on
Not able to run the code as in build errors? Run time errors?
Logical errors?
As you can see, i am trying to store a string of dynamic size. And cant able to do that. i wish to implement that, this is just an example code.

I think you have to pass ios::trunc flag to the fstream constructor so that the file will be created if it doesn't exist.

data.write(reinterpret_cast<const char *>(&A),sizeof(ABC));
This will just copy the bytes of A to the file. This works for simple types but will not work when you have pointers and dynamic allocations.

std::string contains at least one pointer internally that points to the characters. Only the pointer will be written to file. The actual character data will not be written to file. When you later try to read the string from the file the internal string pointer will be set but the memory has probably been deallocated already and might be used by other objects and so things will go bad, and possibly result in a crash.

It's better to read and write each class member separately and handle non-POD types like std::string in a way that makes sense for that type.

One way to write a std::string to file is to first write the size of the string to the file, and then write the characters.
1
2
3
4
std::string str = "Blahaha";
std::size_t size = str.size();
file.write(reinterpret_cast<const char *>(&size), sizeof(size));
file.write(someString.data(), size);
To read the string back from file you first read the size of the file and resize the string. After that you can read the characters.
1
2
3
4
5
std::string str;
std::size_t size;
file.read(reinterpret_cast<char *>(&size), sizeof(size));
str.resize(size);
file.read(&str[0], size);
Last edited on
well, in actual code i am using many dynamic data structures and some of them can not be fixed due to demand of program. And if i write these for all such cases it'll be too hectic.

I found "Json" somewhere, i need to know if that'll help or not? Any of you have some idea on it?
Topic archived. No new replies allowed.