469,643 Members | 1,486 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

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

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 !
Jul 21 '05 #1
2 1527
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

"zjut" <zj**@discussions.microsoft.com>
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 !

Jul 21 '05 #2
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
"zjut" <zj**@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
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 !

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Nader | last post: by
6 posts views Thread by SevDer | last post: by
7 posts views Thread by BillG | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.