1

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);
        }

    }

1 Answers1

0

You are simply not releasing the resources to the stream and in-turn not releasing the file handle.

Don't do this

StreamWriter fileWrite = new StreamWriter(...)

Always use a using statement on an IDisposable when possible, or (if you really need to) call Close/Dispose manually. In-turn this will (in this case) close the stream and dispose the object, releasing the operating system file handle (which is most likely your problem)

using(StreamWriter fileWrite = new StreamWriter(...))
{
    // code here
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I changed to the using statement which solved my path problem, but the initial information is being thrown out upon program close, and the user input is still not writing to the text file unfortunately, any ideas? – user12515401 Mar 17 '20 at 01:05
  • @user12515401 programs dont do things unless you have told them to do them. So are you sayign when you close your app, there is nothing in the file (you have checked this)? – TheGeneral Mar 17 '20 at 01:08
  • Yes, when I close the application all of the initial information (that was already in the text file, not the user input) is completely cleared from the file. The user input however, is still never entering the text file at all. – user12515401 Mar 17 '20 at 01:12
  • @user12515401 the code that is doing this is likely not shown. You could be calling writeReadTextFile again on close or deleting the file. However its impossible to know. What i would suggest is debugging this with the step debugger, check the file manually, and working out what and when things are happening. as it stands your new question is unanswerable because we cant see the code. If all else fails, create a simpler example even in a console app – TheGeneral Mar 17 '20 at 01:34