1

Thank you for all your suggestions!

I'm really confused as to why this still isn't working, the 'Customers.txt' is just included in the solution and it opens it fine with the StreamReader, this is my full code :/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace CustomerDetails
{
class Program
{
    class userDetails
    {
        public static List<string> firstName;
        public static List<string> lastName;
        public static List<string> telNumber;
        public static List<string> birthDate;
        public static List<string> postCode;
        public static string userDecision;
    }

    static void Main(string[] args)
    {
        int x = 1;
        userDetails.firstName = new List<string>();
        userDetails.lastName = new List<string>();
        userDetails.birthDate = new List<string>();
        userDetails.telNumber = new List<string>();
        userDetails.postCode = new List<string>();

        while (x == 1)
        {
            Console.WriteLine("------------------------------");
            Console.WriteLine("       CUSTOMER DATABASE      ");
            Console.WriteLine("------------------------------");
            Console.WriteLine("1.) Add Customer(s)");
            Console.WriteLine("2.) List Customers");
            Console.WriteLine("3.) Exit");
            Console.WriteLine("------------------------------");
            userDetails.userDecision = Console.ReadLine().Trim().ToUpper().Replace(" ", "");
            if (userDetails.userDecision == "1" ||
                userDetails.userDecision == "2" ||
                userDetails.userDecision == "3")
                break;
            else
                Console.Clear();
        }

        if (userDetails.userDecision == "3") { Environment.Exit(0); }

        Console.Clear();
        Console.WriteLine("------------------------------");
        Console.WriteLine("       CUSTOMER DATABASE      ");
        Console.WriteLine("------------------------------");

        if (userDetails.userDecision == "1")
        {
            int y = 0;

            while (y > -1)
            {
                string input;
                Console.Clear();
                Console.WriteLine("------------------------------");
                Console.WriteLine("         NEW CUSTOMER         ");
                Console.WriteLine("------------------------------");
                Console.Write("First Name: ");
                userDetails.firstName.Add(Console.ReadLine());
                Console.Write(" Last Name: ");
                userDetails.lastName.Add(Console.ReadLine());
                Console.Write("       DOB: ");
                userDetails.birthDate.Add(Console.ReadLine());
                Console.Write("Tel Number: ");
                userDetails.telNumber.Add(Console.ReadLine());
                Console.Write(" Post Code: ");
                userDetails.postCode.Add(Console.ReadLine());
                Console.WriteLine("------------------------------");
                int e = 0;
                while (e == 0)
                {
                    Console.Write("Add Another? Y/N:");
                    userDetails.userDecision = Console.ReadLine().ToUpper();
                    if (userDetails.userDecision == "Y" || userDetails.userDecision == "N")
                        e = 1;
                }
                if (userDetails.userDecision == "N")
                {
                    break;
                }
            }

            StreamWriter fileWriter = new StreamWriter(File.Open("Customers.txt", FileMode.Append));

            int v = 0;

            foreach (string element in userDetails.firstName)
            {
                fileWriter.WriteLine("/-----------\\");
                fileWriter.WriteLine(userDetails.firstName[v]);
                fileWriter.WriteLine(userDetails.lastName[v]);
                fileWriter.WriteLine(userDetails.postCode[v]);
                fileWriter.WriteLine(userDetails.birthDate[v]);
                fileWriter.WriteLine(userDetails.telNumber[v]);
                fileWriter.WriteLine("\\-----------/");
                v++;
                Console.WriteLine("DOING.");
            }
            fileWriter.Dispose();
            fileWriter.Close();
            Console.WriteLine("DONE.");
            Console.ReadLine();
        }
        // LIST CUSTOMER DETAILS

        //else if (userDetails.userDecision == "2")
        //{
        //    StreamReader fileReader = new StreamReader("Customers.txt");
        //    string currentLine = "";

        //    while (currentLine != null)
        //    {
        //        currentLine = fileReader.ReadLine();

        //        if (currentLine != null) {
        //            if (currentLine != "/-----------\\") {
        //                if(currentLine == "\\-----------/")
        //                    Console.WriteLine();
        //                else
        //                Console.WriteLine(currentLine); } }
        //    }
        //    fileReader.Close();

        //}
        //Console.ReadLine();
    }
}
}
JustBenji
  • 73
  • 6
  • 1
    Are you sure you want to be repeatedly opening the file *in a loop*? Could you try to come up with a short but *complete* example that we could actually compile ourselves (e.g. I've no idea what `userDetails` actually is) – Damien_The_Unbeliever Oct 16 '14 at 06:40
  • 1
    Are you sure it's not writing to a file in a location you're not looking in? SInce you're not specifying the path, it opens the file in whatever the app considers to be its current directory (not necessarily the directory it runs in). – Erik Funkenbusch Oct 16 '14 at 06:46
  • 1
    Unrelated: your `userDetails` seems to be an object containing several lists. It looks like a better way to structure your data would be to have a `UserDetail` class with the needed properties (`firstName`, `lastName` etc.) as single values, and then have `userDetails` be one single list containing objects of type `UserDetail`. – Corak Oct 16 '14 at 06:47
  • Btw.: @ErikFunkenbusch is right. I just copied your code and it works. But it writes into a `Customer.txt` in the "current directory" of the application. Most likely in the `Debug` or `Release` folder of the project. – Corak Oct 16 '14 at 09:32

2 Answers2

2

You are opening the file as many times as your loop runs.

You need to open the file then enter your loop code, then make sure it closes.

StreamWriter file= new StreamWriter(File.Open("fileName.txt", FileMode.CreateNew));

foreach (string element in userDetails.firstName)
{
   file.WriteLine("testing 1 10 11");

}

file.Close();
file.Dispose();

It seems that using will close the file for you, but I still prefer file.Close() and file.Dispose() until I read more on using.

apxcode
  • 7,696
  • 7
  • 30
  • 41
0

Change the

using (StreamWriter fileWriter = new StreamWriter("Customers.txt"))

line to

using (StreamWriter fileWriter = new StreamWriter(@"C:\Customers.txt"))

if you see a file on c:\ then the problem is the file is being written but not where you expect. Next to the executable is the common location for code like this, but not the only one.

cjb110
  • 1,310
  • 14
  • 30
  • It would be better to recommend a path that's generally writeable - such as their own user directory; the root of the C drive requires admin/UAC rights which they may not be running under. – Damien_The_Unbeliever Oct 16 '14 at 07:16
  • true...but then you tell me the literal for a writeable path on their system? :) Without using some of the framework functions to determine the desktop/documents, which tbh I think would cloud the actual problem. – cjb110 Oct 16 '14 at 08:39
  • Yes, but "no file found on C drive and now it's throwing an exception" introduces a new problem for the OP to diagnose. – Damien_The_Unbeliever Oct 16 '14 at 09:05