我刚从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# (特别是这个二进制文件读取程序,以创建符号的频率并存储在链接列表中)吗?,谢谢帮助
编辑:尝试按照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#的初学者
发布于 2014-03-10 10:22:35
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);
}
}https://stackoverflow.com/questions/22297106
复制相似问题