473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1683
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then...
3
by: David N | last post by:
I got a solution that contains about 30 projects, three of which cannot be open. When I open the project, I always receive the error message "Unable to get the project file from the Web Server" ...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
2
by: Edward Lee | last post by:
Hi, I have a string of XML in VB dot net. What I want to do is simply dump this string programatically into an IE browser so I can view it as its raw data form... Is there any way to do this...
2
by: zjut | last post by:
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 ////////////////////
6
by: SevDer | last post by:
Is there a way to test guid string? I want to do it without try catch block to save on performance. Thanks in advance. -- SevDer
7
by: BillG | last post by:
Hi, Does anyone know of a site or have code for a function that will generate a random string or random number? I need one where I can tell it what type of value I need and where I can set the...
13
by: xzzy | last post by:
None of the following properly do the VB.net double quote conversion because all of the following in csharp convert to \" instead of just a double quote: " I have tried: char...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.