Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件处理中的错误_C#_File Handling - Fatal编程技术网

C# 文件处理中的错误

C# 文件处理中的错误,c#,file-handling,C#,File Handling,我目前正在尝试使用流编写器和流阅读器将数据输入到文本文件中,以输出信息,在本例中为名字、姓氏和年龄。然后,我希望将这些信息输出到下表中: 名字姓氏年龄 等等等等等等 但是我在解决这个问题时遇到了一些问题 请查看下面的代码,有人能解释为什么我会收到以下错误消息吗 错误1使用未分配的局部变量“firstname” 错误2使用未分配的局部变量“lastname” 错误3使用未分配的局部变量“age” 使用系统; 使用System.Collections.Generic; 使用System.Linq;

我目前正在尝试使用流编写器和流阅读器将数据输入到文本文件中,以输出信息,在本例中为名字、姓氏和年龄。然后,我希望将这些信息输出到下表中:

名字姓氏年龄
等等等等等等
但是我在解决这个问题时遇到了一些问题

请查看下面的代码,有人能解释为什么我会收到以下错误消息吗

错误1使用未分配的局部变量“firstname”

错误2使用未分配的局部变量“lastname”

错误3使用未分配的局部变量“age”

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序1
{//开始名称空间
班级计划
{//开始上课
静态void Main(字符串[]参数)
{//启动主
//声明字符串
String firstname;//将名称声明为字符串
String lastname;//将姓氏声明为字符串
字符串年龄;//将年龄声明为字符串
int Counter;//将计数器声明为整数
FileInfo fleNames=newfileinfo(“Names.txt”);//使用Names.txt作为文件名创建fleNames作为对象
StreamWriter swrNames=fleNames.CreateText();//通知fleName创建文本
用于(计数器=0;计数器<1;计数器++)
{
Write(“输入您的名字:”;//使用streamwriter命令写入用户的名字
firstname=Console.ReadLine();
swrNames.WriteLine(firstname);//这指示将在文本文件中存储第一个名称的位置
swrNames.Flush();
Console.Write(“输入您的姓氏”);//使用streamwriter命令写入用户的名字
lastname=Console.ReadLine();
swrNames.WriteLine(lastname);//这指示姓氏将存储在文本文件中的位置
swrNames.Flush();
Console.WriteLine(“输入您的年龄”);//使用streamwriter命令写入用户的名字
age=Console.ReadLine();
swrNames.WriteLine(age);//这指示年龄将存储在文本文件中的位置
swrNames.Flush();
Console.Clear();
}
swrNames.Close();
字符串名称表;
StreamReader swreNames=File.OpenText(“Names.txt”);
而((NamesTable=swreNames.ReadLine())!=null)
{
Console.WriteLine(名称表);
}
swreNames.Close();
Console.ReadLine();
FileInfo fleNamesTwo=新文件信息(“NamesTwo.txt”);
StreamWriter swrNamesTwo=null;
//------选中“附加到文件”--------------
if(fleNames.Exists==true)
{
swrNames=fleNames.AppendText();
}
else/--创建一个新的文本文件
{//声明字符串
字符串名称;//名称
字符串姓氏;//姓氏
字符串年龄;//年龄
swrNamesTwo=fleNamesTwo.CreateText();
控制台。写(“输入您的名字”);
Name=Console.ReadLine();
swrNamesTwo.WriteLine(名称);
swrNamesTwo.Flush();
控制台。写(“输入您的姓氏”);
姓氏=Console.ReadLine();
swrNamesTwo.WriteLine(姓氏);
swrNamesTwo.Flush();
Console.WriteLine(“输入您的年龄”);
Ages=Console.ReadLine();
swrNamesTwo.WriteLine(年龄);
swrNamesTwo.Flush();
Console.Clear();
}
Console.Clear();
控制台。设置光标位置(15,2);
Console.Write(“--Names Table--”);
控制台。设置光标位置(10,4);
控制台。写(“名字”);
控制台。设置光标位置(28,4);
控制台。写(“姓氏”);
控制台。设置光标位置(48,4);
控制台。写入(“年龄”);
Console.ReadLine();
做
{
//使用Streamreader命令读取文件
swrNames.WriteLine(firstname);//从filenames.txt读取名字
Console.SetCursorPosition(10,计数器+6);//在表设置中对齐名字
swrNames.WriteLine(lastname);//从filenames.txt读取姓氏
控制台。设置光标位置(28,计数器+6);
swrNames.WriteLine(age);//从文件名.txt读取age
控制台。设置光标位置(48,计数器+6);
WriteLine({0}{1}是{2}岁),名字,姓氏,年龄);
Console.Clear();//清除屏幕
Console.ReadLine();
}while((firstname=swreNames.ReadLine())!=null);//从文本文件中写出输入
}
}
}
好吧,你们这些乐于助人的人让我克服了错误!!谢谢:)

然而,(总是有一个然而!!!) 我仍然无法让流编写器/读取器在表中显示结果。我让表格显示在结果之后,但表格是空白的,除了表格标题都在它们整洁的占位符中! 任何人……:):)

在C#中,错误
使用未分配的局部变量
表示您试图使用尚未给定值的变量。与其他一些语言不同的是,C#要求您在使用变量之前保证已为变量赋值(甚至为null)——它不会将变量初始化为默认值(如VB.NET)或留下垃圾值(如C++)

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

        String firstname = string.empty;
        String lastname = string.empty; 
        String age = string.empty; 
int Counter=0;
for (Counter = 0; Counter < 1; Counter++)
string firstname = string.empty; // And so on..