我正在嘗試使用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