How to store variables in.txt files

Asked

Viewed 579 times

3

I am a C beginner and I am looking to study how to let my program already configured every time it runs.

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

For example, "remember" what was the last DIR in which my program was "reading", when it is re-experiencing.

how to save and recover var "dir" in a file?

3 answers

3

  • 2

    Hello @Astorga, thanks for the help, but I’m looking for "a technique" most commonly used to work with files saving and recovering variables... for example how to find 1 of the 100 variables I have stored, means?

  • @Camilayamamoto in this case you can search for serialization, I think it will make more sense for you. Arquivos . conf are usually simple to interpret: the characters # are ignored and parameter names are separated by an arbitrary number of spaces up to the first character. If you can already read and write to files, interpreting a file . conf shouldn’t be a problem for you ;)

  • but anyway, I’ll have to "scan" the file.. looking for the string of my var and what it is "matched" to? right?... laborious... neh?

  • That’s precisely what any parser will do for you. What usually happens is that they expose this setting in dictionary format or some simpler structure. Depending on its purpose, libconfig can help you (despite doing much more than just that).

  • 1

    Wow? Why did you get hurt? Your answer wasn’t worth? Because it was a great help to me (although it didn’t solve my problem)! Thank you, I think you’ve stayed on topic!

  • 1

    @Astorga proposed to help and your answer cannot be considered wrong, so vote +1

  • 1

    good attitude! thanks! @Wendell... just one comment.. I have seen a certain "strange thing": the staff negatively what has to do with the subject, and positive responses totally outside the scope... including some offensives of so aggressive that are.... pity...

Show 2 more comments

3

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

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

To not have to reinvent the wheel it is possible to use a standard format such as JSON and "ini", but in this case it will be worth using an external library to read and write the files, because 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.

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

  • 1

    then I would have to "look" for a string in the file, which is my variable, and then "read" that who after it (the value of var)? all written on the nail? How sad...

  • 1

    @Camila Yamamoto: I updated my answer, please take a look.

  • 1

    Wow, I get it, it’s gonna take work

  • 1

    @Camila Yamamoto: Go. :)

  • 1

    @Camila Yamamoto: but look at the edition I made in the reply.

  • 1

    Thanks! I read everything! I’m already studying!

Show 1 more comment

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.