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

12 Years
Discussion Span
Last Post by Ancient Dragon

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.