An Issue With Writing In A File In C#

I developed a simple program that asks the user if he wants to add a new record to a file or display all the records contained in the file, but I seem to have a problem in writing to the file. The file is created but still nothing is written in it. The reading function works fine since I wrote something in the file to see if it works.

Here is the code...

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

namespace F01
{
    class Program
    {
        static int ID;
        static String Name;
        static void Main(string[] args)
        {
            FileStream MyFiler = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamReader FileReader = new StreamReader(MyFiler);
            StreamWriter FileWriter = new StreamWriter(MyFiler);
            Console.WriteLine("Please Select One Of The Following Options... ");
            Console.WriteLine("1. Enter A New Record In The File.");
            Console.WriteLine("2. Read All The Records From The File.");
            int Choice = int.Parse(Console.ReadLine());
            if (Choice == 1)
            {
                Console.WriteLine("Enter The ID: ");
                ID = int.Parse(Console.ReadLine());
                FileWriter.WriteLine(ID);
                Console.WriteLine("Enter The Name: ");
                Name = Console.ReadLine();
                FileWriter.WriteLine(Name);
            }

            else if (Choice == 2)
            {
                FileWriter.Close();
                String fileText = File.ReadAllText("MyFile.txt");
                for (int i = 0; i < fileText.Length; i++)
                {
                    Console.Write(fileText[i]);
                }
            }
            FileReader.Close();
        }
    }
}

Thanks in advance :)

Jon Skeet
people
quotationmark

You're not closing the writer in the only situation in which you're using it - "choice 1". Basically the data is being buffered, and lost when the process exits because you haven't closed the writer.

I would strongly advise you to:

  • Only open the file when you need to
  • Use local variables instead of class variables unless you need them in other methods
  • Use the File static methods to make all of this easier
  • Use using statements to clean up resources at the end of them (if you need a writer or a stream at all, that is...)
  • Break your code up into methods for separate operations
  • Name your variables in camelCase rather than PascalCase
  • Print your file a line at a time rather than a letter at a time

So for example:

using System;
using System.IO;

class Program
{
    const string FileName = "MyFile.txt";

    static void Main(string[] args)
    {
        Console.WriteLine("Please Select One Of The Following Options... ");
        Console.WriteLine("1. Enter A New Record In The File.");
        Console.WriteLine("2. Read All The Records From The File.");
        int choice = int.Parse(Console.ReadLine());
        switch (choice)
        {
            case 1:
                AddEntry();
                break;
            case 2:
                ReadFile();
                break;
            default:
                Console.WriteLine("Sorry, that's not a valid option");
                break;
        }
   }

   static void AddEntry()
   {
       Console.WriteLine("Enter the ID:");
       int id = int.Parse(Console.ReadLine());
       Console.WriteLine("Enter the name:");
       string name = Console.ReadLine();
       File.AppendAllLines(FileName, new[] { id.ToString(), name });
   }

   static void ReadFile()
   {
       foreach (var line in File.ReadLines(FileName))
       {
           Console.WriteLine(line);
       }
   }
}

people

See more on this question at Stackoverflow