1

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

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 written in 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)

11
  • What are you expecting it to do? And where is the code to implement whatever behavior you are expecting? Commented Nov 23, 2013 at 22:35
  • Basically you have to either recursively serialize your nested structs, or instead of going too deep simply write some value that corresponds to NULL... Commented Nov 23, 2013 at 22:35
  • I want the obj1[0].content Address to be written in file. Commented Nov 23, 2013 at 22:37
  • What's recursively serializing nested structs? Commented Nov 23, 2013 at 22:37
  • Welcome to Stack Overflow. Please read the About page soon. Your question title says C; your tags say C++ — and since your code uses the >> input operators, it is written in C++. Please be careful and consistent in your tagging, and generally avoid dual tagging with C and C++. That often brings down the ire of those who look at questions — they are very different languages, and an appropriate answer for C is often inappropriate for C++, and a good answer for C++ often simply doesn't work (as in, 'will not compile') in C. Commented Nov 23, 2013 at 22:45

2 Answers 2

3

Cast this to an uint32 (or uint64 on a 64-bit system). But probably it is not what you want, because there is very few cases if a pointer needs to be written in a file.

Sign up to request clarification or add additional context in comments.

1 Comment

Or, use the integral type that's the right size: uintptr_t.
1

This error occurs because you did not overload the >> operator to be used with your custom object. The operator can't know how to read your data structure from a file and the opposite is true (it wouldn't know how to output it to a file either).

One option that is available to you if you want to read and write data structures from and to files is a process called serialization which takes a data structure and turns it into symbols that can be read / written to a file (roughly).

http://en.wikipedia.org/wiki/Serialization

"recursively serializing nested structs" refers to the fact that your data structure contains pointers to other instances of the same type thus creating a chain. You would need to recursively traverse all nodes in order to obtain all data about your data structure and be able to serialize it.

Also note that it would not be useful to just output the pointer address to a file because there is no guarantee whatsoever about what will be in the memory space next time your program is run. You absolutely have to store the data.

Another way to achieve this would be to store your data as readable text like in an XML file, for example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.