0

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

3
Contributors
4
Replies
6
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

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.

0

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

0

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)

0

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;
   out.close();
}

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.