百分求解,我有一个文件,我如何得到他的内容!

asakao 2008-01-29 05:32:38
文件格式是一个UTF-8编码的单词,加上\0字符结尾,然后两个32位数字。接着就是第二个单词,依次重复。

我现在如何分别得到单词,与后面两位数字,请高手指点,谢谢!
...全文
116 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
asakao 2008-01-30
  • 打赏
  • 举报
回复
谢谢了!
AppFramework 2008-01-29
  • 打赏
  • 举报
回复
因为没有文件,没法调试,你自己调试吧,要注意 ReadInt 如果读取的结果不对,把4个字节的顺序颠倒即可:
------
BlockFileReader.cs:
--------------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WindowsApplication1
{
public class Block
{
public string Word;
public int Int1;
public int Int2;
}

public class BlockFileReader
{
/// <summary>
/// 读取文件内容
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns>字节数组</returns>
private static byte[] ReadFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
try
{
int len = (int)fs.Length; // 文件长度
byte[] content = new byte[len];
fs.Read(content, 0, len);
return content;
}
finally
{
fs.Close();
}
}

public List<Block> Read(string fileName)
{
byte[] content = ReadFile(fileName);
List<Block> result = new List<Block>();

int blockStartIndex = 0;
while ((blockStartIndex = ReadBlock(content, blockStartIndex, result)) < content.Length) ;

return result;
}

private static int ReadBlock(byte[] content, int blockStartIndex, List<Block> result)
{
for (int i = blockStartIndex; i < content.Length; i++)
{
if (content[i] == 0)
{
Block blk = new Block();
blk.Word = Encoding.UTF8.GetString(content, blockStartIndex, i - blockStartIndex);
blk.Int1 = ReadInt(content, i + 1);
blk.Int2 = ReadInt(content, i + 5);
result.Add(blk);
return i + 9;//把指针移动到第二个整数之后
}
}

return content.Length;
}

private static int ReadInt(byte[] content, int startIndex)
{
return (int)((content[startIndex]) | (content[startIndex + 1] << 8) | (content[startIndex + 2] << 16) | (content[startIndex + 3] << 24));
}
}
}


------
调用方法:

private void button2_Click(object sender, EventArgs e)
{
BlockFileReader reader = new BlockFileReader();
List<Block> blks = reader.Read("xxxx.xxx");
}
AppFramework 2008-01-29
  • 打赏
  • 举报
回复
不能像那样做,那样会导致32位数字读取不正确。我现在写一段代码给你。
rainlake 2008-01-29
  • 打赏
  • 举报
回复
System.IO.StreamReader sr = new System.IO.StreamReader("FILE.txt", Encoding.UTF8);
string text = sr.ReadToEnd();
sr.Close();
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(?<word>[^\0]*)\0(?<i1>[0-9]*).*?(?<i2>[0-9]*)");
System.Text.RegularExpressions.Match match = regex.Match(text);
while (match.Success)
{
string word = match.Groups["word"].Value;
int i1 = int.Parse(match.Groups["i1"].Value);
int i2 = int.Parse(match.Groups["i2"].Value);
match = match.NextMatch();
}
asakao 2008-01-29
  • 打赏
  • 举报
回复
还是没有搞定,有没有人可以给我提供一下代码呀,谢谢!
asakao 2008-01-29
  • 打赏
  • 举报
回复
谢谢,AppFramework
我试试!
asakao 2008-01-29
  • 打赏
  • 举报
回复
咦COPY不过来!我帖张图吧
AppFramework 2008-01-29
  • 打赏
  • 举报
回复
思路,
第一步,先把整个文件读取到 byte[] 数组,这个很简单,用 FileStream 即可搞定。
第二步,解析 byte[] 数组,找到 0 之后,用 Encoding.UTF8.GetString(,,) 的方法获得单词;然后用读取后4个字节,用移位的方式整合成32位整数。整合时注意低位在前,高位在后(如果不对,则反过来试试),例如:
int ind = 0 所在的位置索引加1;
int i1 = (bytes[ind])|(byte[ind+1]<<8)|(byte[ind+2])<<16)|(byte[ind+3]<<24);
ind += 4;
int i2 = (bytes[ind])|(byte[ind+1]<<8)|(byte[ind+2])<<16)|(byte[ind+3]<<24);
第三步,递归。
AppFramework 2008-01-29
  • 打赏
  • 举报
回复
思路,
第一步,先把整个文件读取到 byte[] 数组,这个很简单,用 FileStream 即可搞定。
第二步,解析 byte[] 数组,找到 0 之后,用 Encoding.UTF8.GetString(,,) 的方法获得单词;然后用读取后4个字节,用移位的方式整合成32位整数。整合时注意低位在前,高位在后(如果不对,则反过来试试),例如:
int ind = 0 所在的位置索引加1;
int i1 = (bytes[ind])|(byte[ind+1]<<8)|(byte[ind+2])<<16)|(byte[ind+3]<<24);
ind += 4;
int i2 = (bytes[ind])|(byte[ind+1]<<8)|(byte[ind+2])<<16)|(byte[ind+3]<<24);
第三步,递归。
viena 2008-01-29
  • 打赏
  • 举报
回复
用FileStream读到byte数组,然后遍历找到\0的位置,用相应的Encoding得到字符串,数字用Convert转换即可
bird1983 2008-01-29
  • 打赏
  • 举报
回复
读取文件作为字符串的流,然后对这个流进行筛选应该就可以得到你的数据了。
wxg22526451 2008-01-29
  • 打赏
  • 举报
回复
up
ycagri 2008-01-29
  • 打赏
  • 举报
回复
能不能贴一些内容让看看
deknight 2008-01-29
  • 打赏
  • 举报
回复
没有规律?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧