Azul_Echo
Azul_Echo

Reputation: 151

Comparing string to text file

I am currently working on a code where I add a student to a text file of students and make other calculations. The problem I'm having is I have to sort the new student into the text file so he is in the correct place.

StreamWriter changeFile = new StreamWriter("Students.txt", true);
if (pos > 0)
{
    changeFile.Close();
}
else
{
    changeFile.WriteLine(newStudent);
    changeFile.Close();
}

using (StreamReader streamReader = new StreamReader("Students.txt"))
{
    string text = streamReader.ReadToEnd();
}

So far I have managed to change the text file into a string to compare the 2, but it is in the wrong place. What code would I use to make the StreamWriter compare the newStudent string to the text file so it can put it in the correct order? P.S. The text file has over 10,000 students in it.

These are the first 5 lines of the text file:

students (LIST
(LIST (LIST 'Abbott 'A_____ 'J ) 'NONE '[email protected] 2.3073320999676614 )
(LIST (LIST 'Abbott 'B_____ 'Y ) 'NONE '[email protected] 3.1915725161177115 )
(LIST (LIST 'Abbott 'R_____ 'Y ) 'NONE '[email protected] 3.448215586562192 )
(LIST (LIST 'Abel 'H_____ 'Y ) 'NONE '[email protected] 3.2517764202656974 )
 ) ) <- this is at the end

Upvotes: 1

Views: 170

Answers (3)

Daniel Gabriel
Daniel Gabriel

Reputation: 3985

Assuming the text file contains one student per line, you could read the text file into a list of strings, then add the new student then sort it and write it back to the file.

var allStudents = new List<string>(File.ReadAllLines("Students.txt"));
allStudents.Add(newStudent);
allStudents.Sort(allStudents);
File.WriteLines("Students.txt", allStudents);

This is inefficient because every time it reads and writes the whole file. If you have a choice, consider using a database like others have suggested.

EDIT:

Since the first line of your file is not a student, you should remove it before sorting and re-add it later.

You can remove that line like this:

var line = allStudents[0];
allStudents.RemoveAt(0);

And re-add it like this:

allStudents.Insert(0, line);

Upvotes: 0

Anthony
Anthony

Reputation: 396

something like

var Students = File.ReadAllText("Students.txt").Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList().Union(new string[] { newStudent }).ToList().Sort();
File.WriteAllLines("Students.txt", Students);

might work for you

Upvotes: 0

Andrew Cooper
Andrew Cooper

Reputation: 32596

Simple method is:

  1. Split the string at the line breaks
  2. Create a List containing the source lines
  3. Add the new student to the list
  4. Sort the list
  5. Write the list to the file

Alternately:

  1. Split the string at the line breaks
  2. Create a List containing the source lines
  3. Write lines back to the file util you find the position for the new record
  4. Write the new record
  5. Write the rest of the records.

Or, even better for large files:

  1. Read the source file line-by-line
  2. Write lines to a temporary destination file
  3. When you find the correct place to insert the new record, write it to the dest file.
  4. Continue copying lines from source to destination
  5. Delete the source file and rename the destination file to take its place.

Upvotes: 2

Related Questions