如何在csharp中将字符串添加到大文件中! [英] How to add a string to a big file in csharp !

查看:72
本文介绍了如何在csharp中将字符串添加到大文件中!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件中添加一个字符串,文件是逐个字母的!对于

例如:

以下文件是一个大文件

//////////////// //////

abort

black

白菜



鸡蛋

fly

..

..

//////////// ////////

现在我想添加爸爸进去 !就在卷心菜之后在前面

的狗!由于文件中有这么多单词所以我需要采用二进制搜索来找到位置!


///< summary>

///想要从给定文件中找到单词

///< / summary>

///< param name ="? ">< / param>


私人布尔找到(字符串字)

{

if(word == null)

{

抛出新的ArgumentNullException(" word为null。);

}


StreamReader sr = new StreamReader(file.FullName); //文件是对象

FileInfo

lock(this)

{

//检查单词是在第一个!

string str = sr.ReadLine();

if(str == null)

{

返回false;

}

if(string.Compare(str.Trim(),word))

返回true; < br $>
}


//二进制搜索开始

FileStream fs = File.OpenRead(file.FullName);

long lower = 0;

long upper = fs.Length - 1;

while(lower< = upper)

{

长指数=(低+上)/ 2;

fs.seek(指数,SeekOrigin.End);


//读掉一条不完整的行

str = fs.Read();

////我不知道如何设置Read()的参数以便它可以读一个




//如果它是文件的结尾,该行可能为null

int t = str == null? -1

:string.Compare(word,str.trim());

//找到它

if(t == 0 )

{

返回true;

}

if(t> 0)

{

lower = index + 1;

}

else

{

upper = index - 1;

}

}

}


即方法和我的问题的功能是

1:FileStream是否适合它?

2:string.Compare适合它吗?

3:有什么方法可以做得更好吗?


of than!

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

推荐答案

" zjut" < ZJ ** @ discussions.microsoft.com>在消息中写道

新闻:A2 ********************************** @ microsof t.com ...
"zjut" <zj**@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
我想在文件中添加一个字符串,文件是逐个字母的!对于
例如:
以下文件是一个大文件
//////////////////////
abort
黑色
白菜
鸡蛋



//////////// ////////
现在我想添加爸爸进入它!
I 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 !




鉴于:

*您的文件(old_file)按字母顺序排列

* old_file非常大


必需:

*在正确的位置添加单词(new_word)到old_file。

解决方案:

*按顺序读取old_file(" word_read")并将word_read写入new_file

* if(new_word> word_read)和(new_word< word_read + 1)然后将其推入



Given:
* Your file ("old_file") is in alphabetical order
* old_file is immensely big

Required:
* Adding word ("new_word") to old_file at the right place.

Solution:
* Sequentially read old_file ("word_read") and write word_read to new_file
* If (new_word > word_read) and (new_word< word_read+1) then shove it in


" zjut" < ZJ ** @ discussions.microsoft.com>在消息中写道

新闻:A2 ********************************** @ microsof t.com ...
"zjut" <zj**@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
我想在文件中添加一个字符串,文件是逐个字母的! for
I want to add a string to the file and the file is sort by letter! for




如果你遇到算法问题,请告诉我,我会发一个

的例子。该示例将按字母顺序排列的短文件排序为按字母顺序排列的非常大的字母顺序文件。顺便说一句,WordPerfect可以处理非常大的文件(超过100,000个单词)。 Microsoft Word不能。



If you are having problems with the algorithm let me know and I will post an
example. The example sorts a short alphabetically ordered file into a very
big alphabetically ordered file. By the way, WordPerfect can deal with
immensely big files (100,000+ words). Microsoft Word can''t.


我看到另一个NG成员已经给你一个可能的

解决方案,但我不觉得这将是一个最佳的解决方案...你

真的有几个不同的选项,都围绕着

同样的原则......首先,你知道新的大小的单词

你正在排序到位,所以你会想要打开然后再增加

这个数量的文件。这是为了确保您在搜索时可以复制

其余文件。对于

健全性检查,请继续检查第一个和最后一个要素

确定这不是一件小事。


好​​的,二进制搜索将涉及,将文件减半,

你可以根据长度做这个,然后寻找那个位置。

一旦你完成了这个,你就会向后走,并且在你遇到任何一方的换行符之前向前推进
。这将是你的* * *
,你将比较它并继续将文件切成两半(也就是二进制搜索)的过程。 ..一旦你找到了你的插入位置

,你就会做大的缓冲区拷贝(4K

可能是最好的)移动所有最终元素的字节进入你在开始时分配的
空间。完成后,将

字写入到位。你刚刚设置了一个插入位置。


如果你有多个单词要合并,那么合并排序和其他

启发式算法就会发挥作用。得到你的基本算法,然后想一下关于重构的




-

贾斯汀罗杰斯

DigiTec Web Consultants,LLC。

博客: http:// weblogs。 asp.net/justin_rogers


" zjut" < ZJ ** @ discussions.microsoft.com>在消息中写道

新闻:A2 ********************************** @ microsof t.com ...
I see another NG member has already given you a possible
solution, but I don''t feel it would be an optimal solution... You
really have a couple of different options that all revolve around the
same set of principles... First, you know the new size of the word
you are sorting into place, so you''ll want to open and then grow
the file by that amount. This is to make sure you can copy the
rest of the file around while you are doing your searching. For a
sanity check, go ahead and check the first and last element to make
sure this isn''t a trivial case.

Okay, the binary search is going to involve, cutting the file in half,
you can do this based on length, and then seeking to that location.
Once you''ve done that, you are going to walk backwards and
forwards until you encounter newlines on either side. This''ll be
your *word*, and you''ll compare it and continue the process of
cutting the file in half (aka a binary search)... Once you''ve found
your insertion location, you are going to do large buffer copies (4K
is probably best) of bytes moving all of the end elements into that
space you allocated in the beginning. With that done, write your
word into place. You''ve just managed an in place insertion.

If you have multiple words to merge, then merge sorting and other
heuristics come into play. Get your basic algorithm and then think
about refactoring.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"zjut" <zj**@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
我想在文件中添加一个字符串,文件是逐个字母的!对于
例如:
以下文件是一个大文件
//////////////////////
abort
黑色
白菜
鸡蛋



//////////// ////////
现在我想添加爸爸进去 !就在卷心菜之后并且在狗的前面!因为文件中有这么多单词所以我需要采用二进制搜索来找到位置!

///< summary>
///想找到这个单词来自给定文件
///< / summary>
///< param name ="?">< / param>

私人博尔找到(字符串)
{
if(word == null)
{
抛出新的ArgumentNullException(" word is null。);
}

StreamReader sr = new StreamReader(file.FullName); //文件是对象的文件信息
锁定(这个)
//
//检查单词是否在第一个!
字符串str = sr.ReadLine() ;
if(str == null)
{
返回false;
}
if(string.Compare(str.Trim(),word))
返回true;
}
//二进制搜索开始
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);

//读掉一条不完整的行
str = fs.Read();
////我不知道怎么设置read()的参数,以便它可以读取

//如果它是文件的结尾,该行可能为null
int t = str ==空值 ? -1
:string.Compare(word,str.trim());
//找到它
if(t == 0)
{
返回true ;
}
if(t> 0)
{
lower = index + 1;
}

{
upper = index - 1;
}
}
}

这是方法的功能,我的问题是
1:FileStream是合适的在它?
2:string.Compare适合它吗?
3:有什么方法可以做得更好吗?

总而言之!
I 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 !



这篇关于如何在csharp中将字符串添加到大文件中!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆