-1

我有两个类“allmethods.cs”和“caller.cs”

我在“allmethods.cs”类中有两个方法,分别是“WritingMethod”和“ReadingMethod”

程序应该从文本文件中写入和读取。当我调用“WritingMethod”时它写得很顺利,但是当我调用“ReadingMethod”时它显示为空,就好像文本文件中没有数据一样。

我无法识别代码中的问题,如果有人帮助我识别问题,我会很高兴。

这是我的代码:

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();
    }
}

非常感谢。等待您的答复。

4

2 回答 2

2

看来您还没有设置变量output。你已经设置了line变量。

    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();
    }

我修改的内容从 更改outputline

希望能帮助到你。

于 2013-04-26T01:23:56.503 回答
1

这是您的一系列问题的整体解决方案:

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;
}

我建议准备完整的代码,不要问重复的问题。

于 2013-04-26T03:25:50.657 回答