C# 在c语言中读取二进制未知大小的文件# 我刚从C++转到C语言。我已经在C++中做了一些任务,现在我必须在C语言中翻译。
我正在经历一些问题 我必须找到二进制文件中符号的频率(这是唯一的参数,因此不知道它的大小/长度)。(这些频率将进一步用于创建C# 在c语言中读取二进制未知大小的文件# 我刚从C++转到C语言。我已经在C++中做了一些任务,现在我必须在C语言中翻译。,c#,c++,algorithm,binaryfiles,huffman-code,C#,C++,Algorithm,Binaryfiles,Huffman Code,我正在经历一些问题 我必须找到二进制文件中符号的频率(这是唯一的参数,因此不知道它的大小/长度)。(这些频率将进一步用于创建哈夫曼树) >强>我在C++中做的代码如下:< /强> 我的结构是这样的: struct Node { unsigned int symbol; int freq; struct Node * next, * left, * right; }; Node * tree; 我是这样读文件的: FILE * fp;
哈夫曼树
)
<> >强>我在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,谢谢,但是在C++代码中我用了“AGV”,它是从main中提取出来的,就像“int main(int ARC,char *ARGV)”。我的意思是它有双指针,但我们不能在c中使用*。那么这个argv在c#中是如何工作的呢?第二个问题是,我必须在create_freq()函数中传递这个“ch”来创建节点吗?对于我这个C#程序员来说,你能解释一下为什么要使用
int b
(甚至注释“not a byte”)?你为什么不解释原因就评论node:notabyte
?@user234839-你没有具体说明文件名的来源,C++的argv[1]
将是args[0]
。只要它是一个字符串。@ThorstenDittmar-这很有趣,但是名为ReadByte()的方法实际上返回一个int
。因此,特殊值-1
可用于EOF。这个答案给出了处理这种编码的基本模式。@HenkHolterman谢谢!我从未注意到这一点,因为我在现实生活中从未使用过ReadByte
。我宁愿使用int b
(甚至注释“not a byte”)?你为什么不解释原因就评论node:notabyte
?@user234839-你没有具体说明文件名的来源,C++的argv[1]
将是args[0]
。只要它是一个字符串。@ThorstenDittmar-这很有趣,但是名为ReadByte()的方法实际上返回一个int
。因此,特殊值-1
可用于EOF。这个答案给出了处理这种编码的基本模式。@HenkHolterman谢谢!我从未注意到这一点,因为我在现实生活中从未使用过ReadByte
。我宁愿使用int b
(甚至注释“not a byte”)?你为什么不解释原因就评论node:notabyte
?@user234839-你没有具体说明文件名的来源,C++的argv[1]
将是args[0]
。只要它是一个字符串。@ThorstenDittmar-这很有趣,但是名为ReadByte()的方法实际上返回一个int
。因此,特殊值-1
可用于EOF。这个答案给出了处理这种编码的基本模式。@HenkHolterman谢谢!我从未注意到这一点,因为我在现实生活中从未使用过ReadByte
。我宁愿使用int b
(甚至注释“not a byte”)?你为什么不解释原因就评论node:notabyte
?@user234839-你没有具体说明文件名的来源,C++的argv[1]
将是args[0]
。只要它是一个字符串。@ThorstenDittmar-这很有趣,但是名为ReadByte()的方法实际上返回一个int
。因此,特殊值-1
可用于EOF。这个答案给出了处理这种编码的基本模式。@HenkHolterman谢谢!我从未注意到这一点,因为我在现实生活中从未使用过ReadByte
。我宁愿使用Read
来读取整个缓冲区。好吧,我错了。请尝试fp=System.IO.File.OpenRead(args[1])
@HenkHolterman error CS1061:TypeSystem.IO.FileStream'不包含
Readbyte'的定义,并且找不到类型为System.IO.FileStream'的扩展方法Readbyte'(是否缺少using指令或程序集引用?)函数是ReadByte
,而不是ReadByte
。好的,我错了。请尝试fp=System.IO.File.OpenRead(args[1])
@HenkHolterman error CS1061:TypeSystem.IO.FileStream'不包含
Readbyte'的定义,并且找不到类型为
System.IO.FileStream'的扩展方法Readbyte'(是否缺少using指令或程序集引用?)函数是ReadByte
,而不是ReadByte
。好的,我的
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);
}
}