C# 在C中从内存映射文件中查找元素#
我需要在内存映射文件中找到某些元素。我已经设法映射了文件,但是在查找元素时遇到了一些问题。我的想法是将所有文件元素保存到一个列表中,然后在该列表上搜索 如何创建一个函数,返回一个包含映射文件所有元素的列表C# 在C中从内存映射文件中查找元素#,c#,visual-studio-2010,memory-management,C#,Visual Studio 2010,Memory Management,我需要在内存映射文件中找到某些元素。我已经设法映射了文件,但是在查找元素时遇到了一些问题。我的想法是将所有文件元素保存到一个列表中,然后在该列表上搜索 如何创建一个函数,返回一个包含映射文件所有元素的列表 // Index indicates the line to read from public List<string> GetElement(int index) { } 我映射的文件是UTF-8 ASCII文件。没什么奇怪的 我所做的: var list = new
// Index indicates the line to read from
public List<string> GetElement(int index) {
}
我映射的文件是UTF-8 ASCII文件。没什么奇怪的
我所做的:
var list = new List<string>();
// String to store what we read
string trace = string.Empty;
// We read the byte of the pointer
b = _accessor.ReadByte(_pointer);
int tracei = 0;
var traceb = new byte[2048];
// If b is different from 0 we have some data to read
if (b != 0)
{
while (b != 0)
{
// Check if it's an endline
if (b == '\n')
{
trace = Encoding.UTF8.GetString(traceb, 0, tracei - 1);
list.Add(trace);
trace = string.Empty;
tracei = 0;
_lastIndex++;
}
else
{
traceb[tracei++] = b;
}
// Advance and read
b = _accessor.ReadByte(++_pointer);
}
}
var list=newlist();
//字符串来存储我们读到的内容
string trace=string.Empty;
//我们读取指针的字节
b=\u访问器.ReadByte(\u指针);
int-tracei=0;
var traceb=新字节[2048];
//如果b与0不同,我们需要读取一些数据
如果(b!=0)
{
而(b!=0)
{
//检查它是否是一条终点线
如果(b=='\n')
{
trace=Encoding.UTF8.GetString(traceb,0,tracei-1);
列表。添加(跟踪);
trace=string.Empty;
tracei=0;
_lastIndex++;
}
其他的
{
traceb[tracei++]=b;
}
//预读
b=_accessor.ReadByte(++_指针);
}
}
代码很难为人类阅读,效率也不高。如何改进它?您正在重新发明StreamReader,它正是您所做的。你真正想要一个内存映射文件的几率很低,它们占用了大量的虚拟内存,只有你以不同的偏移量重复读取同一个文件,你才能获得回报。这是非常不可能的,文本文件必须按顺序读取,因为您不知道行的长度 这使得这一行代码可能是您发布内容的最佳替代品:
string[] trace = System.IO.File.ReadAllLines(path);
什么样的文件?我们需要更多的信息和代码。我从事.NET开发已有10年,今天之前从未听说过MemoryMappedFile。@John MMFs是.NET 4.0的一项功能。
string[] trace = System.IO.File.ReadAllLines(path);