How to READ pointer of a struct type in file in C++

This is a c++ Code. I want to read a address to a pointer. When I try to do so it generates an error. I did is >> obj1[0].contentAdress; in main.

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
struct node3{
    node3(){
        nextContent = NULL;
        for (int i = 0; i<1020; i++)
            content[i] = '\0';
    }
    char content[1020];
    node3* nextContent;
};
//-------------------------------------------------------------------------------------------

struct node1{
    node1(){
        for (int i = 0; i<496; i++)
            fileName[i] = '\0';
    }

    char fileName[496];
    node3* contentAdress;
};
//-------------------------------------------------------------------------------------------

int main(){

    node1 obj1[2097];
    node3 obj3[8192];


    ifstream is("file.txt");
//I want the obj1[0].content Address to be read from file. For that I did:
    is >> obj1[0].contentAdress;// *THIS GENERATES AN ERROR*

    return 0;
}


ERROR:

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'node3 *' (or there is no acceptable conversion)
Last edited on
closed account (S6k9GNh0)
You don't do this. The only time you write something into a file is for caching purposes. Reading a pointer back like this could be disastrous.
Last edited on
So I can't store pointer in file? Or I shouldn't store pointer in file? OR I CAN STORE?
In addition to what computerequip says, if you want to write to a file, then you want an ofstream, not an ifstream.
closed account (S6k9GNh0)
By default, you cannot store a pointer to the a file using C++ fstreams. You shouldn't do that.
SORRY I'm Actually TRYING TO READ POINTER.
Just tell me CAN I READ AN ADDRESS from file INTO A POINTER? WILL IT BE USEFUL?
closed account (S6k9GNh0)
It's wrong on so many levels...
Supposee a data member of a struct is a pointer and I want to store this struct's object's data. How will the object be stored?
You misunderstand. You can, of course, store a pointer in a file, but it would not be useful to do that.

A pointer is the address of a piece of RAM (memory). When the program wrote it to file it was valid.

Since then, anything could have happened. The program could have been stopped and restarted. The computer could have been rebooted. The file could have been obtained from a different computer. Etc.

The point is that that particular location in memory might not be associated with or used by your program any more, or even if it is, it might not be used for the same kind of data any more.

Writing a pointer to a file doesn't save the data the pointer addresses. It only saves useless information.

1
2
3
4
5
6
struct student
{
  int grade_level;                                    
  char* name;
};
Here's a struct.
It contains an integer 
and a pointer to a name.

Let's create some data and simply dump
it directly to file (the wrong way).
 
student johnny = { 5, "Johnny Test" };                
myfile contains:
1
2
myfile.write( (char*)&johnny, ... ); // here's the foo
5: an integer
?: another integer

In the file, we have failed to save Johnny's name. So anyone else trying to read the file has no way to get his name back -- especially if said person is on another computer.

Hope this helps.
Thanks Duoas; so it's impossible to store an object (of a struct) perfectly having a pointer as member variable?
Last edited on
It's possible, but it means you have to provide some sort of organization to your file.

For your data, you could organize your file thus:

unsigned integer:   number of node1's

(
char[496]:          node1.filename
unsigned integer:   node1.contentAddress (this is an index into this the next list)
)
...  (repeat as many times as there are 'node1's)

unsigned integer:   number of node3's

(
char[1020]:         node3.content
unsigned integer:   node3.nextContent (this is an index into this list)
)
...  (repeat as many times as there are 'node3's)

The indices are simply numbers like 0, 1, 2, ... that index a particular node in its list.

For example, if node1.contentAddress (in the file) is 2, then the, content can be found in the third (index == 2) element of the list of node3 items in the file.

To put it another way, instead of writing a pointer to memory to file, write an index to the item in the file.

This takes a little care when writing and reading the file, but it is not difficult.

Hope this helps.

[edit] Fixed typo
Last edited on
Thanks a lot Duoas, I don't know how to thank you! Its awesome!
Topic archived. No new replies allowed.