#Best way to save and load data to and from file. (Beginner)

8 messages · Page 1 of 1 (latest)

echo reef
#

I am working on a program where I need to load and save some int arrays. The basic structure I need is: int, int, int, int[256] repeated within the file as many times as I need (realisticaly about 2000x).

When reading I want to search in the file by the first three int values and if they exist I want to load the int array. If they don't exist I just want to load all 0.

When writing I want to find if the three integers exist, if they do I replace the array, if they don't I want to append a new entry.

"Human readable" format would be preferred (direct binary file would be human readable in this case as I encode data directly into the binary of the int)

I am pretty new to C++ so please be kind and don't expect I know anything 😉

autumn knotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

lapis wren
#

I would just write a function that writes out your data structure as 259 ints per line.
Then write a function that reads the file line by line until it finds the three ints.
Like don't worry about doing anything fancy for now

echo reef
#

Thanks. I have actually never used file access in c++. What libraries and functions should I look into to achieve this?

lapis wren
#

Probably fstreams

#

They can act like std::cin and std::cout

autumn knotBOT
#

@echo reef Has your question been resolved? If so, type !solved :)

echo reef