Managed C ++. There is data: data groups and in each group integer data. For example: Plant-> 5 tables 4 chairs 2 cabinets Office-> 10 tables 8 chairs 3 cabinets

What is the best way to organize the storage of this data, the subsequent writing to a file and the possibility of reading in the future in the same ordered list? Those. Let's say there will be a list of groups (office, factory) in a comboBox, and when choosing, we received data on the number of chairs, cabinets from a saved file.

I remembered where I had a hitch, why I didn’t take structures right away. Data: office, factory dynamic. I do not know how many of these places will be. How to be?

  • Give your data structures in the C program. Then it will be possible to think specifically about their writing-reading. - avp

1 answer 1

A textbook option is to create structures with the appropriate fields, fill them in and save to the file as binary data with fixed-size chunks, then read.

Write example

struct Data { char Place [20]; int Chair; int Tables; }; struct Data Work = {"Завод", 100, 50}; struct Data Office = {"Оффис", 70, 40}; FILE* f = fopen ("database", "wb"); fwrite (&Work, sizeof(struct Data), 1, f); fwrite (&Office, sizeof(struct Data), 1, f); fclose (f); 

If there is a lot of data, it is better to organize a database like MySQL

  • "and save to file as binary data in fixed-size chunks, then read." It is possible an example, links, in more detail? - Maxim Tsybanov
  • Sample entry added to your answer. I hope you will figure out how to read such a file. - skegg
  • Thank. Because I switched to the EPO recently, for me it is important. I heard that whole structures can be written down, but I didn’t imagine what it looked like. In what form they are written to the file. - Maxim Tsybanov 5:03
  • Well, this is not OOP. This is in C style. Can be written using I / O class objects. But generally, if you want to do something serious, use normal databases. - skegg
  • This is not serious. There is training. And I realize that this is not the PLO. My study just started with C #, where everything is much more humane than in managed c ++ in my opinion :) - Maxim Tsybanov 2011 at 6:01