I am at a loss as to what more I can do here. I have attempted to use namespace StreamReadWrite but that did not work. I have tried to use file.Close(); in different areas but the most that would happen is I'd get "Success" written in the console, nothing displayed from my list and the text file completely empty. I'm not sure if there are several small errors I'm making, but I have been working on this for days and just cannot get the user input to write to the file. Any help is absolutely appreciated! I have written notes to hopefully explain well enough what each part of my code is referencing.
public void writeReadTextFile(Car c) //another form is passing the user input into my list Car to this function
{
try
{
//string variables are all members of Car class
String vin;
String make;
String model;
String year;
String color;
char delim = ',';
StreamWriter fileWrite = new StreamWriter("../../Resources/CarInfo.txt");
foreach (Car car in myCars)
{
fileWrite.WriteLine(car.getVin());
fileWrite.Write(delim); //all attributes separated by comma in text file
fileWrite.Write(car.getMake());
fileWrite.Write(delim);
fileWrite.Write(car.getModel());
fileWrite.Write(delim);
fileWrite.Write(car.getYear());
fileWrite.Write(delim);
fileWrite.Write(car.getColor());
}
StreamReader file = new StreamReader("../../Resources/CarInfo.txt");
String line = file.ReadLine();
int comma;
Car newCar;
while (line != null)
{
//parsing data from text file which works when information is manually entered into text file
comma = line.IndexOf(delim);
vin = line.Substring(0, comma);
line = line.Substring(comma + 1);
comma = line.IndexOf(delim);
make = line.Substring(0, comma);
line = line.Substring(comma + 1);
comma = line.IndexOf(delim);
model = line.Substring(0, comma);
line = line.Substring(comma + 1);
comma = line.IndexOf(delim);
year = line.Substring(0, comma);
line = line.Substring(comma + 1);
color = line;
newCar = new Car(vin, make, model, year, color);
myCars.Add(newCar);
line = file.ReadLine();
Console.WriteLine(line);
}
Console.WriteLine("Success"); // test
}
catch (Exception e)
{
Console.WriteLine("Error loading file: " + e.Message);
}
}