简体   繁体   English

在C#中读取C ++。 我知道我需要字节缓冲区,但还不足够

[英]fread c++ in c#. I know I need byte buffer but not quite there

hey everyone I am new to object oriented programming and I am trying to transfer some c++ code into c#. 大家好,我是面向对象编程的新手,我正在尝试将一些c ++代码转换为c#。 I am trying to translate: 我正在尝试翻译:

fread(top,sizeof(int),16,stream);
first = top[1];
second = top[2];

and so on..... 等等.....

where top is: static int top[16]; top在哪里:static int top [16];

the stream file is a .eng file that I would like to convert to a .csv file. 流文件是一个.eng文件,我想将其转换为.csv文件。 So I want to read the .eng file to convert it. 所以我想阅读.eng文件进行转换。

I currently have 我目前有

if (fs.CanRead)
        {
            byte[] buffer = new byte[fs.Length]; 
            int bytesread = fs.Read(buffer, 0, buffer.Length);
            char[] CharTest = (Encoding.ASCII.GetChars(buffer, 0, bytesread));
            string bytesString = Encoding.ASCII.GetString(buffer, 0, bytesread); 
            Console.WriteLine(CharTest);
            //Console.WriteLine(bytesString);

            byte[] top = new byte[16];
            first = top[1];

so I am able to read my fs file and I have the charTest as the entire .eng file. 因此我能够读取我的fs文件,并将charTest作为整个.eng文件。 Although on the c++ line it is separated into 16 tops. 尽管在c ++上它分成16个顶部。 I do not understand how the c++ does this. 我不明白c ++如何做到这一点。 I am mainly confused on the sizeof(int) part. 我主要对sizeof(int)部分感到困惑。 I have the ability to read the entire file yet not sure where to separate to get the 16 and build the top array 我有能力读取整个文件,但不确定在哪里分开以获取16并构建顶部数组

Here's some documentation about fread : 这是有关fread的一些文档:

http://en.cppreference.com/w/cpp/io/c/fread http://en.cppreference.com/w/cpp/io/c/fread

So, fread s first argument is the place in memory into which the file will be read. 因此, fread第一个参数是文件在内存中的存放位置。 The second is the size in bytes of each object that is read from the file, then the third is the number of objects. 第二个是从文件中读取的每个对象的大小(以字节为单位),然后第三个是对象的数量。 And the last is the stream. 最后是流。

For instance, fread(buffer, sizeof(double), 12, stream) , means read 12 objects the size of a double from stream into buffer. 例如, fread(buffer, sizeof(double), 12, stream) ,装置读取的对象12的尺寸的的double来自流进缓冲器中。

static int header[16]; means an array of 16 ints with internal linkage (that last part isn't necessarily something you need to care about at this stage). 表示由16个具有内部链接的整数组成的数组(最后一部分不一定是您在此阶段需要关注的东西)。

The code I would write is: 我要编写的代码是:

using (var fs = File.OpenRead("somefile.eng"))
using (var br = new BinaryReader(fs))
using (var csv = new StreamWriter("output.csv", false, Encoding.ASCII))
{
    // Note that the array is useless, because we write the csv
    // one int at a time!
    int[] row = new int[16];

    while (true)
    {
        // used for skipping the ; at before the first element
        bool first = true;

        // Note that the file must be composed of only
        // blocks of 16 int32 . No dangling byte
        for (int i = 0; i < 16; i++)
        {
            row[i] = br.ReadInt32();

            // You are skipping top[0]
            if (i == 0)
            {
                continue;
            }

            // No ; before the first element
            if (first)
            {
                first = false;
            }
            else
            {
                csv.Write(';');
            } 

            csv.Write(row[i]);
        }

        // End of file
        if (br.PeekChar() == -1)
        {
            break;
        }

        csv.WriteLine();
    }
}

There is a quite good BinaryReader class that can be used for reading binary data from a Stream (file in this case). 有一个很好的BinaryReader类,可用于从Stream (在这种情况下为文件)读取二进制数据。 Then you can write the csv by using the StreamWriter class. 然后,您可以使用StreamWriter类编写csv。

Normally at this point I'd throw a tirade about Encoding.ASCII and about writing manually a CSV file instead of using a library, but this being a numerical only CSV then it isn't too much a bad thing to do as written. 通常,在这一点上,我会讲一些关于Encoding.ASCII和手动编写CSV文件而不是使用库的文章,但这只是一个数字CSV,所以编写起来并不是一件坏事。

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

相关问题 将dll方法从C ++导出到C#。 为什么我需要:“extern”C“” - Export dll method from C++ to C#. Why I need: “ extern ”C“ ” C#。 当我需要单击时如何避免双击 - C#. How to avoid double click when I need single click 需要C#中的多重继承功能。 我究竟做错了什么? - Need multiple inheritance functionality in C#. What am I doing wrong? 需要帮助在 C# 中重构 MongoDB 查询。 我可以“转换”一个 BsonDocument 到 IMongoQuery 吗? - Need help refactoring a MongoDB query in C#. Can I “convert” a BsonDocument to IMongoQuery? C#。 通过电子邮件发送byte [],然后将其检索 - C#. Sending byte[] via email and then retrieve it C / C ++ - 如何将Buffer.BlockCopy(C#)转换为C / C ++ - C/C++ - How can I convert Buffer.BlockCopy (C#) to C/C++ 我复制了一个C#的文件,现在不能删了? - I copied a file with C#. Now I can't delete it? 我需要从批处理文件的 output 中获取包含某个单词的行,并将其保存为 C# 中的字符串。 我该怎么做? - I need to get a line that contains a certain word from the output of a batch file and save it as a string in C#. How would I do this? Visual C#中的套接字。 需要帮忙! - Sockets in Visual C#. Need Help! C#。 我如何获得随机 ID 然后获得 largeImgURL - C#. How would I get random ID and then get the largeImgURL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM