[英]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.