我不確定我在這裏做錯了什麼。它編譯並運行,行爲正常,但不會將行寫入文本文件。我有我的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 '[email protected] 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);
}
-1:我不完全確定您在MSDN上閱讀的內容,但是您沒有寫入文件的任何痕跡。請查看以下搜索結果[msdn c#寫入文件](http://www.bing.com/search?q=msdn+c%23+write+to+file),尤其是[如何:寫入文本文件(C#)](http://msdn.microsoft.com/en-us/library/vstudio/8bh11f1k.aspx)。也請儘量讓你的示例更加緊湊(查看http://www.sscce.org獲取指導)。 –