我不确定我在这里做错了什么。它编译并运行,运行正常,但它没有将行写入文本文件。我有我的 FileStream 并在 MSDN 上阅读以尝试自己解决这个问题。当我确实开始工作时,它会用我放入的内容覆盖整个文件。所以我希望也许做这件事4个多月的程序员(比如我)能解释一下。这是我认为相关的一段代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication33
{
class Program
{
static void Main(string[] args)
{
FileStream FireBall = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(FireBall);
string inValue;
string[] values;
double GPA;
double total = 0;
double counter = 0;
double count = 0;
double counti = 0;
double counterr = 0;
List<string> Anderson = new List<string>(); //Anderson
List<string> gpa = new List<string>(); //GPA
List<string> noemail = new List<string>(); // email
List<string> lines = new List<string>();
string newLastName = "'Constant";
string newRecord = "(LIST (LIST 'Constant 'Malachi 'D ) '1234567890 'mdconstant@mail.usi.edu 4.000000 )";
string line;
string lastName;
bool insertionPointFound = false;
for (int i = 0; i < lines.Count && !insertionPointFound; i++)
{
line = lines[i];
if (line.StartsWith("(LIST (LIST "))
{
values = line.Split(" ".ToCharArray());
lastName = values[2];
if (newLastName.CompareTo(lastName) < 0)
{
lines.Insert(i, newRecord);
insertionPointFound = true;
}
}
}
if (!insertionPointFound)
{
lines.Add(newRecord);
}