开发者

A console application used to capture data and save it to a text file...How does one do it?

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

namespace ABC
{
    class Program
    {
        static void Main(string[] args)
        {

            string userInput;
            List<string> a = new List<string>();


            do
            {
                Console.WriteLine(">>> NAME <<<");
                Console.WriteLine("1 - Add");
                Console.WriteLine("0 - Exit");

                //get user's choice/input
                userInput = Console.ReadLine();

                //actions to take after user's choice/input
                switch (userInput)
                {
                    case "1":
                        //Add list to store info
                        Console.WriteLine("B");

                        //capture details
                        a.Add("Name: ");
                        a.Add("Surname: ");
                        a.Add("Address: ");
                        a.Add("Telephone: ");
                        a.Add("Cell: ");
                        a.Add("Email: ");
                        a.Add("Web: ");
                        a.Add("Date: ");

                        foreach (string i in a)
                        {
                            Console.Write(i);
                            Console.ReadLine();
                        }
                        FileStream fs = new FileStream("myfile.txt",FileMode.Create开发者_JS百科,FileAccess.ReadWrite);
                        StringBuilder sb = new StringBuilder();
                        foreach (string str in a)
                            {
                            StreamReader sr = new StreamReader();
                            {
                                sb.AppendLine(str.ToString());
                                sb.Append(sr.ReadToEnd());
                                sb.AppendLine();
                            }
                        }
                        StreamWriter sw = new StreamWriter(@"myfile.txt");
                        sw.Write(sb.ToString());

                        break;
                    case "0":
                        Console.WriteLine("BYE!!!");
                        break;
                    default:
                        Console.WriteLine("{0} is not a valid choice", userInput);
                        break;
                }
                //allow user to see results
                Console.Write("press 'Enter' to continue...");
                Console.ReadLine();
                Console.WriteLine();
            }
            // Keep going until the user wants to quit 
            while (userInput != "0");
        }
    }     
 }


I did not run your code, but:

a) don't forget to close your file.

b) it looks like you always add those "data fields" to you list (do it once, on clear it every time you add a new set)


You got a FileStream and a StreamWriter open on the same file at the same time, this will lead to an error.

Deleting the line

FileStream fs = new FileStream("myfile.txt",FileMode.Create,FileAccess.ReadWrite);

and relplacing

StreamWriter sw = new StreamWriter(@"myfile.txt");
sw.Write(sb.ToString());

with

using (TextWriter tw = File.CreateTex("myfile.txt")) {
    tw.Write(sb.ToString());
}

should do the trick.

Edit: Your program is doing exactly what you told it to do.

So here is the functionality that I think that you want to achieve:

case "1": //Add list to store info Console.WriteLine("B");

//capture details
a.Add("Name: ");
a.Add("Surname: ");
a.Add("Address: ");
a.Add("Telephone: ");
a.Add("Cell: ");
a.Add("Email: ");
a.Add("Web: ");
a.Add("Date: ");

StringBuilder sb = new StringBuilder();
foreach (string i in a)
{
    Console.Write(i);
    var entry = Console.ReadLine();
    sb.AppendFormat("{0}{1}\n", i, entry);
}

using (TextWriter tw = File.AppendTex("myfile.txt")) {
    tw.Write(sb.ToString()); 
    tw.WriteLine("-------------------------------------------------");
}

break;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜