dsij89625 2013-08-01 16:00
浏览 59
已采纳

f#c替代c​​#[关闭]

I'm currently working on a demo file parser for a game I play. I've been looking at the format and the example given which is in PHP (trying to convert it to C#) but it uses fgetc which from what I can tell isn't available in C#, only C++. Any ideas how I can work around this? I've tried using StreamReader.Read() but it doesn't like null values.

The example can be found here if anyone wanted to take a look at the full script to get a better idea of what I'm trying to achieve.

Thanks!

  • 写回答

1条回答 默认 最新

  • douchilian1009 2013-08-01 16:13
    关注

    You'll want to use a BinaryReader to read that file. To read int, you can call ReadInt32. To read their strings, you'll need to read byte-by-byte ReadByte and compare with 0.

    using (var fs = File.OpenRead(filename))
    {
        using (var reader = new BinaryReader(fs))
        {
            // reading happens here
        }
    }
    

    private string ReadString(BinaryReader reader) { byte b; StringBuilder sb = new StringBuilder(); while ((b = reader.ReadByte()) != 0) { sb.Append((char)b); } return sb.ToString(); }

    I made a mistake in converting the code. The file format says that the string will be 260 characters long. So you need to read 260 bytes and then trim the string, like this:

    private string ReadString(BinaryReader reader, int length = 260)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; ++i)
        {
            byte b = reader.ReadByte();
            sb.Append((char)b);
        }
        return sb.ToString().Trim();
    }
    

    To read the header, you would write:

    string hldemo = ReadString(reader, 8);
    

    and to read the host name, for example, it would be:

    string host = ReadString(reader);
    

    That's pretty much an exact duplicate of the php code, I think. See the format document for other string lengths, and look at their sample code. I might have missed something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示