2

Hi guys I am kinda new to C#. I am trying to do some file io stuff. When I read and write it is only doing it on one line. Is there a way to advance the streamreader/streamwriter? It is for a school assignment. Here is what I have so far. Thanks very much.

enter code her class FileIO 
{
    public static void Save(ArrayList vehicleList)
    {

        StreamWriter streamWriter;
        FileStream file = new FileStream(Constants.fileName, FileMode.OpenOrCreate);
        streamWriter = new StreamWriter(file);

        String typeOfVehicle = " ";
        String model = " ";
        String manufactuer = " ";
        Int32 year = 0;
        Int32 vin = 0;
        Double price = 0;
        String purchaseDate = " ";
        Int32 currentOdometerReading = 0;
        Double sizeOfEngine = 0;
        String typeOfMotorCycle = " ";
        Int32 numOfDoors = 0;
        String typeOfFuel = " ";
        Double cargoCapacity = 0;
        Double towingCapacity = 0;

        foreach (CommonItem vehicle in vehicleList)
        {
            typeOfVehicle = vehicle.TypeOfVehicle;
            model = vehicle.Model;
            manufactuer = vehicle.Manufactuer;
            year = vehicle.Year;
            vin = vehicle.Vin;
            price = vehicle.InitialPrice;
            purchaseDate = vehicle.PurchaseDate;
            currentOdometerReading = vehicle.CurrentOdometerReading;
            sizeOfEngine = vehicle.EngineSize;

            streamWriter.Write(typeOfVehicle + "~" + manufactuer + "~" + model + "~" + year.ToString() + "~" + vin.ToString() + "~" + price.ToString() + "~" + purchaseDate + "~" + currentOdometerReading.ToString() + "~" + sizeOfEngine.ToString() + "~");

            switch (typeOfVehicle)
            {
                case "Automobile":
                    numOfDoors = ((Automobile)vehicle).NumberOfDoors;
                    typeOfFuel = ((Automobile)vehicle).TypeOfFuel;
                    streamWriter.Write(numOfDoors + "~" + typeOfFuel + "~");
                    break;
                case "Motorcycle":
                    typeOfMotorCycle = ((Motorcycle)vehicle).Type;
                    streamWriter.Write(typeOfMotorCycle + "\n");
                    break;
                case "Truck":
                    cargoCapacity = ((Truck)vehicle).CargoCapacity;
                    towingCapacity = ((Truck)vehicle).TowingCapacity;
                    streamWriter.Write(cargoCapacity + "~" + towingCapacity);
                    break;
            }

            streamWriter.Write("\n");
        }
        streamWriter.Close();

    }

    public static ArrayList Load()
    {
        ArrayList vehicles = new ArrayList();
        FileStream file = new FileStream(Constants.fileName, FileMode.OpenOrCreate);
        StreamReader streamReader = new StreamReader(file);

        String typeOfVehicle = " ";
        String model = " ";
        String manufactuer = " ";
        Int32 year = 0;
        Int32 vin = 0;
        Double price = 0;
        String purchaseDate = " ";
        Int32 currentOdometerReading = 0;
        Double sizeOfEngine = 0;
        String typeOfMotorCycle = " ";
        Int32 numOfDoors = 0;
        String typeOfFuel = " ";
        Double cargoCapacity = 0;
        Double towingCapacity = 0;

        String tempString = " ";

        while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()))
        {
            Int32 temp;
            String [] split = tempString.Split('~');
            temp = split.Length;

            typeOfVehicle = split[0];
            manufactuer = split[1];
            model = split[2];
            year = Convert.ToInt32(split[3]);
            vin = Convert.ToInt32(split[4]);
            price = Convert.ToDouble(split[5]);
            purchaseDate = split[6];
            currentOdometerReading = Convert.ToInt32(split[7]);
            sizeOfEngine = Convert.ToDouble(split[8]);

            if (typeOfVehicle == "Automobile")
            {
                numOfDoors = Convert.ToInt32(split[9]);
                typeOfFuel = split[10];
                Automobile car = new Automobile(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, numOfDoors, typeOfFuel);
                VehicleCount.IncreaseCarCount();
                vehicles.Add(car);
            }
            else if (typeOfVehicle == "Motorcycle")
            {
                typeOfMotorCycle = split[9];
                Motorcycle bike = new Motorcycle(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, typeOfMotorCycle);
                VehicleCount.IncreaseBikeCount();
                vehicles.Add(bike);
            }
            else
            {
                cargoCapacity = Convert.ToDouble(split[9]);
                towingCapacity = Convert.ToDouble(split[10]);
                Truck truck = new Truck(manufactuer, model, year, vin, price, purchaseDate, currentOdometerReading, sizeOfEngine, cargoCapacity, towingCapacity);
                VehicleCount.IncreaseTruckCount();
                vehicles.Add(truck);
            }
        }

        streamReader.Close();

        return vehicles;
    }
}

}

2 Answers 2

1

When I read and write it is only doing it on one line. Is there a way to advance the streamreader/streamwriter?

Yes - use streamWriter.WriteLine() instead of streamWriter.Write();

What you are writing looks like data separated by "~" - you might want to look into writing out CSV = comma-separated values, which is similar and pretty much standard.

Also instead of ArrayList you should use a strongly typed collection - in your case depending on your class structure you can use List<Vehicle> (if Automobile and Motorcycle both inherit from Vehicle).

Sign up to request clarification or add additional context in comments.

7 Comments

Okay that fixed my writing issue. But when I am reading in it is still only reading in one line
How are you reading it in? What does the data look like if you open it with notepad?
Something like this : there is new lines between each one: Motorcycle~e~s~2000~12~12~1\1\2000~1~1~sport Motorcycle~e~s~2000~12~12~1\1\2000~1~1~sport Motorcycle~e~s~2000~12~12~1\1\2000~1~1~sport
And i have a method called load()
while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine())) This loop is only iterating once.
|
0

The issue is with this line

while (!String.IsNullOrEmpty(tempString = streamReader.ReadLine()))

The loop exits when it meets a blank line because IsNullOrEmpty method returns true. Try below instead..

while ((tempString = streamReader.ReadLine()) != null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.