I am making a text based game and want the user to be able to save. When they save all the variables will be saved to a text file.
I can’t figure out how to take them out of the file and assigning them to specific variables and pointers.
The file will look somewhat like this:
jesse
hello
yes
rifle
0
1
3
20
Is there anyway I can specify what line I want to take out with fscanf? Or do I have to take a different approach?
There is no way to specify what line to read from because the concept of a file stream in C does not explicitly distinguish new lines. They are simply treated as a character. To read from a specific line, you would have to loop forward with
fseekandfgetcuntil you find'\n'at which point you can update some variable that holds the current line number the stream points to.One way around this would be to have information at a fixed offset. For example, say you are storing player information then if you make player information a fixed size X and have the constituent data at fixed indexes into each structure, you can just
fseekto the right location straight away.However, if you have structured data, it may be more suitable to use a format which is able to represent these structures inherently such as XML or JSON.