EmbraceNext EmbraceNext - 4 years ago 110
C++ Question

Intro to programming project. Stuck on creating a data file from a table

I have been given a project for class and am stuck on a part of it. The project has this table in it:

enter image description here

How can I create this information into a sequential file to read into a C++ program? It looks like this would be a 3 dimensional array. Am I looking too far into this?

Answer Source

It depends on what you are trying to do. Typically if you are are just trying to read it in you can store it as a flat file. With flat files you typically have two common formats fixed length record or comma separate (there are other flat file formats you can implement but these are two common fomats).

In fixed length record each record is a fixed length and each field is a fixed length. For example

Turkey             1.00Swiss        .30Roll           .30Condiments         .10
Pastrami           1.30Swiss        .40Roll           .30Condiments         .10

In comma separated language you can do it like this Turkey,1.00,Swiss,.30,Roll,.30,Condiment,.10

With fixed length record the con is that once specified and depending on who is the recipient of the file is your fields can never grow. For example if your price field is five spaces you max value ever would be 99.99 and you would have a hard time ever increasing the field size because it would no longer be compatible if some other application depende on it being 5 characters. Comma separated is a little more flexible because the separator is the comma. With comma separate you can usually have the fields grow in size because your comma is the separator.

There is another option and that is to create a ini type of what I will call Domain Specific format as an example take a look below

Sandwich: Turkey
Deli Slice: Turkey,1.00
Deli Slice: Swiss,.30
Bread: Roll,.30
Condiments: .30

Sandwich: Pastrami
Deli Slice: Pastrami,1.00
Deli Slice: Swiss,.40
Bread: Roll,.30
Condiments: .10

This format is more flexible because you can add more fields. For example lets say you have a new sandwich called a california and it required meat cheese and also avocado. You can extend it like this

Sandwich: California
Deli Slice: Turkey, 1.00
Deli Slice: American, .30
Deli Slice: Avocado, .50
Bread: Roll,.30
Condiments: .10

With this third format and some careful programming your new sandwich will automatically be backwards compatible.

Each of the files takes a different strategy for reading it in. There is something called parsing and each file would have a different approach. In the fixed length record the approach wouldnbe to read each column by character positions. In the comma separated file you would need to tokenize each line read in in C++ you have a strtok but if you use managed C++ .net i believe you have a string.split. With the third option you actually look for keywoards such as "Sandwich", "Deli SLice" etc and then you also have to tokenize the itmes because they

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download