現在、StreamWriterとStreamReaderを使用してテキストファイルにデータを入力し、情報(この場合は名、名前、年齢)を出力しようとしています。次に、これを以下の表に出力したい情報を使用します。
First Name Surname Age
Etc Etc Etc
しかし、私はそれを解決するのにいくつかの問題があります!!
以下の私のコードを参照してください。次のエラーメッセージが表示される理由を誰かが説明できますか?
エラー1割り当てられていないローカル変数「名」の使用
エラー2割り当てられていないローカル変数「lastname」の使用
エラー3割り当てられていないローカル変数「age」の使用
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
}
}
}
わかりました、あなたは親切な人々が私をエラーを乗り越えさせてくれました!! ありがとうございました :)
ただし、(常にあります!!!)ストリームライター/リーダーに結果をテーブルに表示させることはできません。結果の後にテーブルが表示されますが、テーブルの見出しがすべて適切なプレースホルダーにあることを除いて、テーブルは空白です。誰でも..... :):):)