Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have two classes "allmethods.cs" and "caller.cs"

I have two methods in the "allmethods.cs" class which are "WritingMethod" and "ReadingMethod"

The program should write and read from a text file. It writes smoothly when I call the "WritingMethod" but When I call the "ReadingMethod" it shows null as if there is no data in the text file.

I can't identify the problem in my code, I'd be glad if anyone help me identify the problem.

Here is my code:

public class allmethods
{
    private static string Name;
    private static int ID;
    private static int Age;
    private static string Email;
    private static string output;

    public static void WritingMethod()
        {
             int count = 0;
             while (count < 2)
            {
            Console.Write(" Enter your Name: ");
            Name = Console.ReadLine();

            Console.Write(" Enter your ID: ");
            ID = int.Parse(Console.ReadLine());

            Console.Write(" Enter your Age: ");
            Age = int.Parse(Console.ReadLine());

            Console.Write(" Enter your E-mail: ");
            Email = Console.ReadLine();



        StreamWriter Sw = new StreamWriter("fileone.txt", true);
        string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
        + Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);
        Console.WriteLine(output);      
        Sw.WriteLine(output + Environment.NewLine);
        Console.ReadLine();

        Sw.Close();
        count++;
        }

    }

    public static void ReadingMethod()
    {

        FileStream fsr = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read);     
        StreamReader Sr = new StreamReader(fsr);       
        string line = Sr.ReadLine();
        Console.WriteLine("--Reading The File--" + Environment.NewLine + output + Environment.NewLine);
        Console.ReadLine();

        Sr.Close();
        fsr.Close();
    }
}

Thank you very much. Waiting for your answers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
249 views
Welcome To Ask or Share your Answers For Others

1 Answer

It seems that you have not set the variable output. You have set line variable.

    public static void ReadingMethod()
    {

        FileStream fsr = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read);     
        StreamReader Sr = new StreamReader(fsr);       
        string line = Sr.ReadToEnd();
        Console.WriteLine("--Reading The File--" + Environment.NewLine + line + Environment.NewLine);
        Console.ReadLine();

        Sr.Close();
        fsr.Close();
    }

What I have modified is changed from output to line.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...