Streamreader doesn't read data in text file C#

狂风中的少年 提交于 2019-12-13 10:33:56

问题


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.


回答1:


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.




回答2:


This is the total solution for the series of your questions:

public partial class AllMethods {
    static T ReadData<T>(String prompt, T value) {
        Console.Write(prompt);
        return (T)Convert.ChangeType(Console.ReadLine(), typeof(T));
    }

    public static void WritingMethod(int timesToInput) {
        using(var sw=new StreamWriter(path, true))
            for(var list=items.ToArray(); timesToInput-->0; ) {
                var inputs=new Dictionary<String, object>();

                for(var i=0; i<list.Length; ++i) {
                    var item=list[i];
                    var prompt=String.Format(" Enter your {0}: ", item.Key);

                    inputs.Add(
                        item.Key, AllMethods.ReadData(prompt, item.Value));
                }

                var output=String.Format(format, inputs.Values.ToArray());
                sw.WriteLine(output+Environment.NewLine);
                Console.WriteLine(output);
                Console.ReadLine();
            }
    }

    public static void ReadingMethod() {
        var textFromFile=
            String.Join(Environment.NewLine, File.ReadAllLines(path));

        Console.WriteLine(
            "--Reading The File--"+Environment.NewLine+textFromFile);

        Console.ReadLine();
    }

    static AllMethods() {
        items=new Dictionary<String, object>();

        // add any item with name and type default value
        items.Add("Name", default(String));
        items.Add("ID", default(int));
        items.Add("Age", default(int));
        items.Add("Email", default(String));

        var prompts=items.Select(
            (item, index) => String.Format("{0}: {{{1}}}", item.Key, index));

        format=
            "Thank you for registration! Your Submitted information are: "
            +Environment.NewLine
            +String.Join(Environment.NewLine, prompts.ToArray());

        path="fileone.txt";
    }

    static Dictionary<String, object> items;
    static String format, path;
}

I'd suggest that to prepare for complete code, and don't ask duplicate questions.



来源:https://stackoverflow.com/questions/16227263/streamreader-doesnt-read-data-in-text-file-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!