Basic Classes concepts are creating big problems.

This Question is removed. It's no longer available.
Last edited on
It's probably because you're creating it on the stack and running out of memory.

Create it on the heap instead.

 
fileSystem *obj = new fileSystem();
Good. but when I'll write this object using
1
2
         ofstream os("file.dat", ios::binary);  
               	os.write( reinterpret_cast<char*>(&obj), sizeof(obj) );

then only 4 bytes will be written but I want a file of 10mb to be written. How to solve this issue?
Because you're passing 4 bytes as the stream size argument.

 
sizeof( obj );


That's sizeof on a pointer, which is four bytes.

 
sizeof( *obj );


That's sizeof on the object the pointer is pointing to.
Dear that is not possible because when I do:
1
2
ofstream os("file.dat", ios::binary);
os.write( reinterpret_cast<char*>(*obj), sizeof(*obj) );


I get:
error C2440: 'reinterpret_cast' : cannot convert from 'fileSystem' to 'char *'
1> Conversion requires a constructor or user-defined-conversion operator, which can't be used by const_cast or reinterpret_cast


If you can tell how to make this thing possible then that would really be a useful thing for me.
Last edited on
Yeah. You don't want to dereference the pointer when you cast it. You only need to dereference it on the second argument.

You know that the file created is going to be useless, right? I'm assuming you're doing this as some sort of exercise to create a random file of a certain size.
Hmm impressive; but I think I will be able to load this file into an object once I've written my object, so this file won't be useless. Am I right? Will I be able to load it into an object of sameType?
Yeah, there are ways to read files into objects. I was referring to the fact that there is no meaningful data in the node objects at the moment.
@OP

1
2
ofstream os("file.dat", ios::binary);  
               	os.write( reinterpret_cast<char*>(&obj), sizeof(obj) );


You absolutely must not do this. It will not work as you expect.

Your 'filesystem' class contains a pointer and therefore is not a POD type, so a straight dump of its memory to a file will not preserve all the information you are trying to preserve. When you load the file later, you will have a stray pointer that points to random garbage in memory.

Further explanation / reading:
http://www.cplusplus.com/articles/oyhv0pDG/
@Disch, O no it is problematic for me if it's true (I'll check it when I fill my object with data). Is there a solution to write an object of say 10MB and then read it back in another object of same type?
The size of the object does not matter, as long as the object contains only POD types. Your 'filesystem' class contains a pointer, which is not a POD type.

You can't just write a pointer to a file. You need to follow the pointer and write the data it points to. In your case, since it points to a node which I'm assuming is part of a tree... this is nontrivial. You'll have to walk through the tree and save the data (but not the pointer) from each node.

This will take planning. I recommend you map out the desired spec for your file format and save each field individually rather than trying to write objects in bulk.

Further reading and info:
http://www.cplusplus.com/articles/oyhv0pDG/
http://www.cplusplus.com/articles/DzywvCM9/


Alternatively, you could look into serialization libs like boost::serialize.. but those require you to do something similar.
Ok, thanks a lot for your time iHutch and Disch. Some people are really nice.
Last edited on
Great article, Disch. Cheers.

Edit: Please don't delete posts. They might be helpful to other users in the future.

Original 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
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

/*
1MB = 1048576 bytes
no. of 500bytes block in 1mb = 1mb / 500 = 2097.152

8mb = 8388608 bytes
no. of 1024bytes block in 8mb = 8192
*/

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct node3{
	node3(){
		nextContent = NULL;
	}

	char content[1020];
	node3* nextContent;
};
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct node1{
	char fileName[496];
	node3* contentAdress;
};
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
struct node2{
	node3* contentAddress;
};
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class fileSystem{
public:
private:
	node1 obj1[2097];
	node2 obj2[8192];
	node3 obj3[8192];
};
//*************************************************************************************************************************************************************************
//*************************************************************************************************************************************************************************
int main(){
	fileSystem obj;
        return 0;
}
Last edited on
Topic archived. No new replies allowed.