Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

class - Streamreader doesn't read data in text file C#

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...