tags:

views:

159

answers:

2

See the program below:

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

namespace FileOperation1
{
    class FileMain
    {
        static void Main(string[] args)
        {
            FileMain fm = new FileMain();
            char ch = fm.Menu();
            while (ch != '0')
            {
                switch (ch)
                { 
                    case '0':
                        break;
                    case '1':
                        //Console.WriteLine("This featute is not implemented till now.");
                        break;
                    case '2':
                        Console.Write("Enter the name of the file: ");
                        String FileName = Console.ReadLine();// ReadLine() method is not workin here
                        FileOperation Fo=new FileOperation();
                        Console.WriteLine("\n" + Fo.FileRead(FileName, FileMode.Open, FileAccess.Read));
                        break;
                    case '3':
                        //Console.WriteLine("This featute is not implemented till now.");
                        break;
                    default:
                        Console.WriteLine("Invalid choice. Enter again.");
                        break;
                }
                ch = fm.Menu();
            }
        }
        private char Menu()
        {
            Console.WriteLine("\n\t***File Operations***");
            Console.WriteLine("1. Create a new file");
            Console.WriteLine("2. Open a file");
            Console.WriteLine("3. Edit an existing file");
            Console.WriteLine("0. Exit");
            Console.Write("\nEnter your choice: "); 
            char ch = Convert.ToChar(Console.Read()); //Read() Method is not working properly
            return ch;
        }
    }
    public class FileOperation
    {
        private String FileRead(FileStream Fs)
        {
            StreamReader Sr = new StreamReader(Fs);
            Sr.BaseStream.Seek(0, SeekOrigin.Begin);
            String str = ""+(Char)Sr.Read();
            String ret = "";
            while (!Sr.EndOfStream)
            {
                ret += str;
                str = ""+(Char)Sr.Read();
            }
            Sr.Close();
            return ret;
        }
        public String FileRead(String FileName, FileMode Fm, FileAccess Fa)
        {
            FileOperation Fo = new FileOperation();
            FileStream Fs = new FileStream(FileName, Fm, Fa);
            String ret = Fo.FileRead(Fs);
            Fs.Close();
            return ret;
        }
    }
}

I am using Visual Studio 2005. Here Console.ReadLine() and Console.Read() functions are not working properly for this program. Why?

+4  A: 

You may want to try Console.ReadKey(true) , this waits for the next key press then continues, the function returns a ConsoleKeyInfo. You could use a switch statement when referring to the ConsoleKey value in the Key property:

    var c = Console.ReadKey();
    Console.WriteLine();
    Console.WriteLine(c.Key.ToString());

    // Prints
    //a
    //A

    switch(c.Key)
    {
        case ConsoleKey.D0:
            //User entered 0
            Console.WriteLine("Exiting...");
            break;
        case ConsoleKey.D1:
            //User entered 1
            Console.WriteLine("You chose to create a new file!");
            break;
        case ConsoleKey.D2:
            //User entered 2
            Console.WriteLine("You chose to open a file!");
            break;
        case ConsoleKey.D3:
            //User entered 3
            Console.WriteLine("You chose to edit an existing file!");
            break;
        default:
            Console.WriteLine("No response for that key");
            break;
    }

    Console.ReadLine();
fletcher
@fletcher: But what is the problem with my program?
chanchal1987
A: 

Replace Console.Read() methods with Console.ReadLine(). See more about Console.Read() and Console.ReadLine() method from MSDN. Edited program is given below:

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

namespace FileOperation1
{
    class FileMain
    {
        static void Main(string[] args)
        {
            FileMain fm = new FileMain();
            char ch = fm.Menu();
            while (ch != '0')
            {
                switch (ch)
                {
                    case '0':
                        break;
                    case '1':
                        //Console.WriteLine("This featute is not implemented till now.");
                        break;
                    case '2':
                        Console.Write("Enter the name of the file: ");
                        String FileName = Console.ReadLine();// ReadLine() method is not workin here
                        FileOperation Fo = new FileOperation();
                        Console.WriteLine("\n" + Fo.FileRead(FileName, FileMode.Open, FileAccess.Read));
                        break;
                    case '3':
                        //Console.WriteLine("This featute is not implemented till now.");
                        break;
                    default:
                        Console.WriteLine("Invalid choice. Enter again.");
                        break;
                }
                ch = fm.Menu();
            }
        }
        private char Menu()
        {
            Console.WriteLine("\n\t***File Operations***");
            Console.WriteLine("1. Create a new file");
            Console.WriteLine("2. Open a file");
            Console.WriteLine("3. Edit an existing file");
            Console.WriteLine("0. Exit");
            Console.Write("\nEnter your choice: ");
            char ch = Convert.ToChar(Console.ReadLine()); //Read() Method is not working properly
            return ch;
        }
    }
    public class FileOperation
    {
        private String FileRead(FileStream Fs)
        {
            StreamReader Sr = new StreamReader(Fs);
            Sr.BaseStream.Seek(0, SeekOrigin.Begin);
            String str = "" + (Char)Sr.Read();
            String ret = "";
            while (!Sr.EndOfStream)
            {
                ret += str;
                str = "" + (Char)Sr.Read();
            }
            Sr.Close();
            return ret;
        }
        public String FileRead(String FileName, FileMode Fm, FileAccess Fa)
        {
            FileOperation Fo = new FileOperation();
            FileStream Fs = new FileStream(FileName, Fm, Fa);
            String ret = Fo.FileRead(Fs);
            Fs.Close();
            return ret;
        }
    }
}
chanchal1987