   memory and stuff
  hi, im learning c++ and i wanted to make a program, to help learn the language. i wanted to know, how can you make a variable keep it's memory after the programis finished. like if 'number' equaled 50, and in the program, changed to 55, how could i keep it as 55 for the next time i run the program?thanks   
Quote:Original post by shred masterhi, im learning c++ and i wanted to make a program, to help learn the language. i wanted to know, how can you make a variable keep it's memory after the programis finished. like if 'number' equaled 50, and in the program, changed to 55, how could i keep it as 55 for the next time i run the program?The most straightforward solution here would be to write any values you want to save to a file at program termination, and read them back in from the same file on program startup. C++ has several file streaming classes which make this process quite easy; just look online or in whatever reference you're using for materials on the ifstream and ofstream classes.   ;  Keywords you probably want to be searching with might be things like stdio and "file input/output"   ;  If you want a quick answer, here it is:You need a variable FILE *fp. Use it to store your number in this fashion:fp=fopen("save.txt","w");fprintf(fp,"%d",number);fclose(fp);When you want to read the saved number:fp=fopen("save.txt","r");fscanf(fp,"%d",&number);fclose(fp);Note that this may act funny if you don't have the save.txt file in your program's main directory. Also, this is just a quick solution, if you want to actualy learn about file input/output check out some of the articles on the main site.   ;Quote:Original post by DJ14IVI3Note that this may act funny if you don't have the save.txt file in your program's main directory.It will only act funny if you attempt to open the file for reading when it doesn't exist.Opening the file with "w" will automatically create the file if it doesn't exist, and if it does, it truncates the file to 0 bytes (blanks out the file).   