i need to write the value of a pointer into file then read it can you help me

Recommended Answers

All 4 Replies

Do you mean the address contained in the pointer or the object to which it points. What kind of pointer do you have in mind? Writing the address to a file is useless because it cannot be simply read back in later and expect it to point to the same place it was originally. The operating system has probably shuffled memory around in the meantime.

thanx for your reply :)
i need the address that the pointer point to ,

To get the address of the pointer depends on how it was initialized.

If it was initialized as a pointer:

int* int_ptr;

To send the address to an ostream just use out << int_ptr; If you don't have a pointer, but only a variable:

int myNumber;

Then to send the address of that variable you use the &.

out << &myNumber;

& = Address(address of)
* = Deferencer(value pointed at by)

Like I said, the address is meaningless in a file. But you can save the object to which it points.

#include <fstream>
#include <string>
using namespace std;

int main()
   string str = "Hello World";
   char *ptr = "another World";
   ofstream out("myfile.txt");
   out << str << endl;
   out << ptr << endl;

The above code does NOT save the addresses of the pointers, but the string to which they point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.