So, I recently wrote a program for a class that involved writing an aggregate class to a file. This particular program had an order object, and each order could have 10 ice cream cones, so a ToString() of the class would look like this:

Order Number: 1
Customer #1: Harrington, Honor

Ice Cream Cone: Scoops: 1, Cone Cost: $1.25, Pickle  
Ice Cream Cone: Scoops: 1, Cone Cost: $1.25, Tangarine   
Ice Cream Cone: Scoops: 1, Cone Cost: $1.25, Nectarine  
Ice Cream Cone: Scoops: 1, Cone Cost: $1.25, Orange   
Ice Cream Cone: Scoops: 1, Cone Cost: $1.25, Chocolate   
Ice Cream Cone: Scoops: 1, Cone Cost: $1.25, Fudge  
Yogurt Cone: Scoops: 1, Cone Cost: $1.25, Pickle  
Yogurt Cone: Scoops: 1, Cone Cost: $1.25, Tangarine  
Yogurt Cone: Scoops: 1, Cone Cost: $1.25, Nectarine   
Yogurt Cone: Scoops: 1, Cone Cost: $1.25, Orange  

Total Price: $12.50

Is there any right way to do this? Would the aggregates be seperated into seperate files? What I ended up doing was attempting to read a certain class from the file, and if an exception occurred I would roll back the file stream and try to read the other class. The order information was there regaurdless.

You are probably right. Any other ways of doing it? Just wondering what other programmers do in this situation.

You could use an Access database. Or XML (xmlreader and xmlwriter). Or just put it in a plain text file like you've done.

Since this is for a class, you can probably do whatever you want, as it is unlikely that you will have that many records. You just need to use 1 or more unique characters as separators. As long as your not accepting Euros (or some other currency that uses commas) you should be fine.

It is unlikely that the file needs to be easily readable, I would only use an blank line to separate records or elmininate blank lines because it will reduce the size of your file. You want something you can easily split the string on--such as a comma. Think of a .csv (comma-delimited) file.

Here's a slight modification (changed first colon to a comma):

    Order Number: 1
    Customer #1: Harrington, Honor
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Pickle  
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Tangarine   
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Nectarine  
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Orange   
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Chocolate   
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Fudge  
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Pickle  
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Tangarine  
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Nectarine   
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Orange  
    Total Price: $12.50

    Order Number: 2
    Customer #1: Harrington, John
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Pickle  
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Tangarine   
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Nectarine  
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Orange   
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Chocolate   
    Ice Cream Cone, Scoops: 1, Cone Cost: $1.25, Fudge  
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Pickle  
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Tangarine  
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Nectarine   
    Yogurt Cone, Scoops: 1, Cone Cost: $1.25, Orange  
    Total Price: $12.50

No one is probably going to be reading your file, except for your program. So you probably could elminate the descriptions. To increase your program efficiency you want to reduce the amount of processing you have to do. In my above example, you'd have to split on the comma then split on the colon. Below you only split on the comma.

You see if the line starts with "Order Number", if so lineCount = 1; If line starts with "Total Price" we have reached the end of the order.

        Order Number: 1
        Customer #1: Harrington, Honor
        Ice Cream Cone, 1, $1.25, Pickle  
        Ice Cream Cone, 1, $1.25, Tangarine   
        Ice Cream Cone, 1, $1.25, Nectarine  
        Ice Cream Cone, 1, $1.25, Orange   
        Ice Cream Cone, 1, $1.25, Chocolate   
        Ice Cream Cone, 1, $1.25, Fudge  
        Yogurt Cone, 1, $1.25, Pickle  
        Yogurt Cone, 1, $1.25, Tangarine  
        Yogurt Cone, 1, $1.25, Nectarine   
        Yogurt Cone, 1, $1.25, Orange  
        Total Price: $12.50

        Order Number: 2
        Customer #2: Harrington, John
        Ice Cream Cone, 1, $1.25, Pickle  
        Ice Cream Cone, 1, $1.25, Tangarine   
        Ice Cream Cone, 1, $1.25, Nectarine  
        Ice Cream Cone, 1, $1.25, Orange   
        Ice Cream Cone, 1, $1.25, Chocolate   
        Ice Cream Cone, 1, $1.25, Fudge  
        Yogurt Cone, 1, $1.25, Pickle  
        Yogurt Cone, 1, $1.25, Tangarine  
        Yogurt Cone, 1, $1.25, Nectarine   
        Yogurt Cone, 1, $1.25, Orange  
        Total Price: $12.50

You probably don't have to be all that concerned about it until you take a "File Structures" class, a database class, or any other class that deals with performance.

Edited 2 Years Ago by cgeier

This article has been dead for over six months. Start a new discussion instead.