Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
fread c++ in c#. I know I need byte buffer but not quite there
大家好,我是面向对象编程的新手,我正在尝试将一些 c++ 代码转换为 c#。 我正在尝试翻译:
fread(top,sizeof(int),16,stream);
first = top[1];
second = top[2];
等等.....
其中 top 是: 静态 int top[16];
流文件是一个 .eng 文件,我想将其转换为.csv文件。 所以我想读取 .eng 文件进行转换。
我目前有
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];
所以我能够读取我的 fs 文件,并且我将 charTest 作为整个 .eng 文件。 尽管在 c++ 行上,它分为 16 个顶部。 我不明白 c++ 是如何做到这一点的。 我主要对大小(int(部分感到困惑。 我有能力读取整个文件,但不确定在哪里分离以获得 16 并构建顶部数组
以下是一些关于fread
的文档:
http://en.cppreference.com/w/cpp/io/c/fread
因此,fread
的第一个参数是文件将被读取到内存中的位置。第二个是从文件中读取的每个对象的大小(以字节为单位(,然后第三个是对象数。最后一个是溪流。
例如,fread(buffer, sizeof(double), 12, stream)
表示将 12 个double
大小的对象从流读取到缓冲区中。
static int header[16];
表示具有内部链接的 16 个整数数组(最后一部分不一定是现阶段您需要关心的(。
我会写的代码是:
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();
}
}
有一个非常好的BinaryReader
类可用于从Stream
(在本例中为文件(读取二进制数据。然后,可以使用StreamWriter
类编写 csv。
通常,在这一点上,我会抛出一篇关于Encoding.ASCII
和手动编写 CSV 文件而不是使用库的长篇大论,但这是一个只有数字的 CSV,那么按照所写的方式做并不是一件坏事。
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- scanf( "%d" , array[i] + 1)是什么意思?为什么+1在那里?
- C++ csv 文件无法正确加载到矢量,它似乎知道对象在那里但不显示它们
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- 如何让 cout 缓冲区在 ubuntu 上刷新
- Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
- 链接 openGL 库失败,因为它无法打开 libgl.so ...但它就在那里
- 带有模板的循环缓冲区在Keil MDK5上是模棱两可的错误?
- C++:使用数组、指针和 for 循环制作直方图。我就在那里,我可能想得太辛苦了
- C 是否具有接口类概念,如果它在那里,那么它与Java接口类别有何不同
- OpenGL - 深度缓冲区在渲染半透明立方体时剪切掉不应该剪切的面
- 无法使用渲染缓冲区在帧缓冲区上绘制
- 为什么静态必须在那里
- "我"的意思,为什么在那里?
- DirectX11 C 着色器缓冲区在多边形布局描述上变为null
- 流缓冲区在C++中的工作原理
- 班级"Oscar"可能在那里做什么?
- 为什么"ostream & os"必须在那里C++操作员过载?
- 用Google协议缓冲区在C 上广播
- 田野不能被重新爱,虽然它显然在那里!(实现地图.h)