我不明白为什么输出文件不包含任何文字? 为什么数组没有写入文件?运行时我没有收到任何错误..
Console.Write("Please enter a name for the homework: ");
hmwk[1].name = Console.ReadLine();
Console.Write("Please enter the subject: ");
hmwk[1].type = Console.ReadLine();
redo:
Console.Write("Please enter the date the homework was set xx/xx/xx: ");
string toconvert = Console.ReadLine();
DateTime temp;
if (DateTime.TryParse(toconvert, out temp)== false)
{
goto redo;
}
DateTime converted = DateTime.Parse(toconvert);
hmwk[1].dateset = converted;
redo2:
Console.Write("Please enter the date the homework deadline is: ");
toconvert = Console.ReadLine();
if (DateTime.TryParse(toconvert, out temp) == false)
{
goto redo2;
}
converted = DateTime.Parse(toconvert);
hmwk[1].deadline = converted;
StreamWriter outfile = new StreamWriter("homework.txt", false);
outfile.WriteLine(hmwk[1].name);
outfile.WriteLine(hmwk[1].type);
outfile.WriteLine(hmwk[1].dateset);
outfile.WriteLine(hmwk[1].deadline);
Console.WriteLine("You have added the homework. Press Enter.");
答案 0 :(得分:4)
可能是因为它不是flushed。你应该Dispose()
StreamWriter,这样它会自动将缓冲的数据刷新到文件中。使用using
处理示例:
using (StreamWriter outfile = new StreamWriter("homework.txt", false))
{
outfile.WriteLine(hmwk[1].name);
outfile.WriteLine(hmwk[1].type);
outfile.WriteLine(hmwk[1].dateset);
outfile.WriteLine(hmwk[1].deadline);
}