我正在尝试使用Stream Writer和Stream Reader将数据输入到文本文件以输出信息,在这种情况下 - 名字,姓氏&年龄。然后,我希望将这些信息输出到如下表格中:文件处理中的错误
First Name Surname Age
Etc Etc Etc
但是,我遇到了一些问题!
请参阅下面的代码,任何人都可以解释为什么我得到以下错误消息?
错误1只使用未分配的局部变量 '姓名'
错误2使用未分配的局部变量的 '名字'
错误3使用未分配的局部变量 '年龄'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{// Start namespace
class Program
{// Start Class
static void Main(string[] args)
{// Start Main
// Declaration Strings
String firstname; // Declares Name as a String
String lastname; // Declares Surname as a String
String age; // Declares Age as a String
int Counter; // Declares Counter as an Integer
FileInfo fleNames = new FileInfo("Names.txt"); // Creates fleNames as an Object, using Names.txt as the file name
StreamWriter swrNames = fleNames.CreateText(); // Informs fleName to Create Text
for (Counter = 0; Counter < 1; Counter++)
{
Console.Write("Enter Your First Name:"); // Writes users first name using Stream Writer Command
firstname = Console.ReadLine();
swrNames.WriteLine(firstname); // This dictates where the first name will be stored in the text file
swrNames.Flush();
Console.Write("Enter Your Surname"); // Writes users first name using Stream Writer Command
lastname = Console.ReadLine();
swrNames.WriteLine(lastname); // This dictates where the last name will be stored in the text file
swrNames.Flush();
Console.WriteLine("Enter Your Age"); // Writes users first name using Stream Writer Command
age = Console.ReadLine();
swrNames.WriteLine(age); // This dictates where the age will be stored in the text file
swrNames.Flush();
Console.Clear();
}
swrNames.Close();
String NamesTable;
StreamReader swreNames = File.OpenText("Names.txt");
while ((NamesTable = swreNames.ReadLine()) != null)
{
Console.WriteLine(NamesTable);
}
swreNames.Close();
Console.ReadLine();
FileInfo fleNamesTwo = new FileInfo("NamesTwo.txt");
StreamWriter swrNamesTwo = null;
// ------------ CHECK APPEND TO A FILE --------------
if (fleNames.Exists == true)
{
swrNames = fleNames.AppendText();
}
else // -- Create a new text file
{ // Declare Strings
String Name; // Name
String Surnames; // Surname
String Ages; // Age
swrNamesTwo = fleNamesTwo.CreateText();
Console.Write("Enter Your First Name");
Name = Console.ReadLine();
swrNamesTwo.WriteLine(Name);
swrNamesTwo.Flush();
Console.Write("Enter Your Surname");
Surnames = Console.ReadLine();
swrNamesTwo.WriteLine(Surnames);
swrNamesTwo.Flush();
Console.WriteLine("Enter Your Age");
Ages = Console.ReadLine();
swrNamesTwo.WriteLine(Ages);
swrNamesTwo.Flush();
Console.Clear();
}
Console.Clear();
Console.SetCursorPosition(15, 2);
Console.Write("--- Names Table ---");
Console.SetCursorPosition(10, 4);
Console.Write("First Name");
Console.SetCursorPosition(28, 4);
Console.Write("Surname");
Console.SetCursorPosition(48, 4);
Console.Write("Age");
Console.ReadLine();
do
{
//Read a File a Streamreader Command
swrNames.WriteLine(firstname); // Reads from first name from file Names.txt
Console.SetCursorPosition(10, Counter + 6); // Aligns first name within table settings
swrNames.WriteLine(lastname); // Reads from last name from file Names.txt
Console.SetCursorPosition(28, Counter + 6);
swrNames.WriteLine(age); // Reads from age from file Names.txt
Console.SetCursorPosition(48, Counter + 6);
Console.WriteLine("{0} {1} is {2} years old", firstname, lastname, age);
Console.Clear(); //Clears Screen
Console.ReadLine();
} while ((firstname = swreNames.ReadLine()) != null); //Writes out the input from the text file
}
}
}
的
好吧,让你有帮助的人让我过去的错误!谢谢:)
但是,(总是有一个!!!) 我仍然无法让流编写器/阅读器在表中显示结果。我得到的结果显示在表格后面,但表格是空白的,除了表格标题全部在整齐的占位符中! ANYONE ..... :) :) :)
初始化变量,为空或的String.Empty – NoviceProgrammer 2013-03-19 14:39:58
你有没有想过'Initializing'字符串变量='string.Empty' '字符串名字=的String.Empty; //将名称声明为字符串' 'string lastname = string.empty; //声明姓氏为字符串' 'string age = string.empty; // De' – MethodMan 2013-03-19 14:41:29
试图理解这是什么...它需要多个修复。为什么要有循环:'for(Counter = 0; Counter <1; Counter ++)'?它只会执行一次。 – 2013-03-19 14:41:29