I am trying to save program data from one execution to another through sending the data to file with fstream. I was just wondering if instead of writing the whole string content to the file, maybe it would be nicer and cleaner to write the strings memory address to file instead, and then retrieve the memory address from file and assigning it to a pointer.
first program should look like this:
string x = "hello world";
ofstream outfile("test.txt");
outfile << &x; //send memory address to file
second program should look something like this:
string *ptr;
int x;
ifstream infile("test.txt"); //file contains memory address
infile >> x;
ptr = x; /*can i tell the pointer that the number is a memory address and not
a integer?*/
Is all this possible? after searching around i got the feeling that im the only one that thought of this.
Having said all that, i"m starting to suspect that maybe the computer reuses memory addresses every time a program starts so the address contents keep on changing? or maybe not...
When a program is terminated, the memory associated with that program is cleaned up by the operating system. This means that the data will no longer exist at that address (or at all) next time you run the program.
You need to save the string contents in one of 2 ways:
1. Save it to a file,
2. Save it to the registry (windows).
This is not possible in general. Most modern operating systems does not allow a process access memory of another process just like that. You would have to use some OS specific function to do this, and in that case there are better ways to communicate between processes like sockets, memory-mapped files, etc.
I get it, thank you all for your help. I was just thinking that keeping all my strings in files and then reloading them looks a little nerdy. Thanks again.