How to add a string to a big file in csharp !

G

Guest

want to add a string to the file and the file is sort by letter! for examply:
the follow file is a big file!
//////////////////////
abort
black
cabbage
dog
egg
fly
////////////////////
and now i want to add "dad" into it ! Just after "cabbage" and at the front
of "dog"! Because of so many word in file so i need to adopt binary search to
find the location !

/// <summary>
/// want to find the word from given file
/// </summary>
/// <param name="?"></param>

private bool find(string word)
{
if (word == null)
{
throw new ArgumentNullException("word is null.");
}

StreamReader sr = new StreamReader(file.FullName); //file is object of
FileInfo
lock(this)
{
//Check the word is in the first!
string str = sr.ReadLine();
if (str == null)
{
return false;
}
if (string.Compare(str.Trim(),word))
return true;
}

// binary search starts
FileStream fs = File.OpenRead(file.FullName);
long lower = 0;
long upper = fs.Length - 1;
while (lower <= upper)
{
long index = (lower + upper) / 2;
fs.seek(index,SeekOrigin.End);

// read off an incomplete line
str = fs.Read();
////i donot know how to set the parameters of Read() so that it can read a
line

// the line might be null if it's the end of file
int t = str == null ? -1
: string.Compare(word, str.trim());
// found it
if (t == 0)
{
return true;
}
if (t > 0)
{
lower = index + 1;
}
else
{
upper = index - 1;
}
}
}

that is the fuction of method and my question is
1: the FileStream is fitable in it ?
2 : string.Compare is fitable in it ?
3: is there any method i can do it better ?

thanx of all !
 
C

Cor Ligthert

Zjut,

It does not matter what program language you use, you can not insert in a
streaming written file than by rewritting the complete file. You can by the
way add to the file, however that is always at the end.

I hope this helps?

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Zjut,
If the file is always sorted, and each word is on a line, I would read the
file, line by line, writing each line to a second file. When I get to the
line where the word belongs I would write the word.

The one caveat being is that a complete copy of the file will be created,
for each word added. If this is a bad thing I would research other
algorithms...

Something like:

Dim word As String = "dad"

Dim input As New StreamReader("words.txt")
Dim output As New StreamWriter("words.output.txt")
Dim line As String = input.ReadLine()

Do Until line Is Nothing
If String.Compare(word, line) < 0 Then
Exit Do
End If
output.WriteLine(line)
line = input.ReadLine()
Loop

output.WriteLine(word)

Do Until line Is Nothing
output.WriteLine(line)
line = input.ReadLine()
Loop

output.Close()
input.Close()

You could then use File.Delete & File.Move to delete the old words list &
replace it with the new words list, possible making a backup of the old list
just in case...

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top