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 !
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 !