开发者

Retaining the value of a variable even after the C program is closed and opened again

I want to save an integer variable, so that it can be retained even after the C program is restarted.

The way is to store it in a file.

But it is just an integer variable. In the file if I write 1000 first and replace it with 12, it saves as 1200, how can I delete the old value which was there in the file and write the new one?

Or 开发者_开发技巧some other way other than files exist?

Thanks Pradeep


When you call fopen to open the file stream, use the "w" mode; this opens the file for writing and truncates it.


You need to truncate the file before writing to it again, this will wipe the contents so you can write the new value.


Add a \n or some other control character to indicate the end of the number. You can use fscanf to read it back.


You might want to use the registry for tasks like this. If using Windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜