Storing Variables in .txt Files

Posted on

Question :

I’m a beginner in C and I’m looking to study how to get my program set up every time it runs.

It seems to me a very important thing, but I found little material.

For example, “remember” which was the last DIR my program was “reading” when it re-run.

How to save and retrieve var “dir” in a file?


Answer :

After a lot of pain, I found this article on VOL … talking about this LibConfuse: link

Has anyone used it yet? knows? recommend?

I’m going to start studying it … who knows, it’s easier “


To write files in C, you can use the functions fopen , fwrite and fclose (included in header stdio.h ).

Here is a practical example of how to write and read data to a file: link


There is no automatic way to do this, it has to be explicitly programmed.

In the normal output of the program you record in a file of your choice the data that you think will be necessary in the next activation of the program, and in the activation you read the file and execute the necessary actions according to what was read , for example, change the current application directory.

In order to avoid reinventing the wheel it is possible to use a standard format such as JSON and “ini”, but in this case it will be worthwhile to use an external library to read and write the files as there are many of these libraries available.

Alternatively you can use the built-in SQLITE database, but in this case the configuration file will not be in text mode. I think this is an interesting alternative.

Obs. the Windows environment already provides functions ready to access “ini” files. So specifically for Windows it is very easy to use “ini” files.


Leave a Reply

Your email address will not be published. Required fields are marked *