繁体   English   中英

在C#中读取二进制大小未知的文件

[英]reading a binary unknown size file in c#

我刚刚从c ++切换到了c#。 我已经在c ++中完成了一些任务,现在我必须在c#中进行翻译。

我遇到了一些问题。

我必须在二进制文件中找到符号的频率(这是唯一的参数,所以不知道它的大小/长度)。(这些频率将进一步用于创建huffman tree )。

我在c ++中执行此操作的代码如下:

我的结构是这样的:

struct Node     
{    
    unsigned int symbol;
    int freq;    
    struct Node * next,  * left, * right;  
};
Node * tree;

我如何读取文件是这样的:

FILE * fp;
fp = fopen(argv, "rb");
ch = fgetc(fp);

while (fread( & ch, sizeof(ch), 1, fp)) {
    create_frequency(ch);
}

fclose(fp);

谁能帮我翻译一下c#中的内容(特别是这种二进制文件读取过程,以创建符号频率并将其存储在链接列表中)? 谢谢您的帮助

编辑:试图根据Henk Holterman在下面解释的内容编写代码,但仍然存在错误,错误是:

error CS1501: No overload for method 'Open' takes '1' arguments
/usr/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
shekhar_c#.cs(22,32): error CS0825: The contextual keyword 'var' may only appear within a local variable declaration
Compilation failed: 2 error(s), 0 warnings

我执行此操作的代码是:

static void Main(string[] args)
{
    // using provides exception-safe closing
    using (var fp = System.IO.File.Open(args))
    {
        int b; // note: not a byte
        while ((b = fp.Readbyte()) >= 0)
        {
            byte ch = (byte) b;
            // now use the byte in 'ch'
            //create_frequency(ch);
        }
    }
 }

与这两个错误相对应的行是:

using (var fp = System.IO.File.Open(args))

有人可以帮我吗? 我是C#的初学者

string fileName = ...
using (var fp = System.IO.File.OpenRead(fileName)) // using provides exception-safe closing
{
    int b; // note: not a byte

    while ((b = fp.ReadByte()) >= 0)
    {
        byte ch = (byte) b;
        // now use the byte in 'ch'
        create_frequency(ch);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM