当前位置:网站首页>C read / write binary file
C read / write binary file
2022-04-23 03:06:00 【Dotnet cross platform】

One option for reading and writing binaries is to use stream types directly ; under these circumstances , It's best to use byte arrays to perform read and write operations . Another option is to use the readers and writers defined for this scenario :BinaryReader and BinaryWriter. They are used in a way similar to StreamReader and StreamWriter, but BinaryReader and BinaryWriter Do not use any encoding . The file is written in binary format instead of text format .

And Stream Different types ,BinaryWriter by Write() Method defines 18 Overloaded versions . Overloaded versions accept different types , This is shown in the following code snippet , It writes double、int、long and string:
public static void WriteFileUsingBinaryWriter(string binFile)
{
var outputStream = File.Create(binFile);
using (var writer = new BinaryWriter(outputStream))
{
double d = 47.47;
int i = 42;
long 1 = 987654321;
string s = "sample";
writer.Write(d);
writer.Write(i);
writer.Write(1);
writer.Write(s)
}
}

To read the file again , have access to BinaryReader. The method defined by this class will read all different types , Such as ReadDouble、ReadInt32、ReadInt64 and ReadString, As shown below :
public static void ReadFileUsingBinaryReader(string binFile)
{
var inputStream = File.Open(binFile, FileMode.Open);
using (var reader = new BinaryReader(inputStream))
{
double d = reader.ReadDouble();
int i = reader.ReadInt32();
long l = reader.ReadInt64();
string s = reader.ReadString();
Console.WriteLine($"d: {d}, i: {i},l: {1},s: {s}");
}
}

The order in which files are read must exactly match the order in which they are written . When creating your own binary format , Need to know what and how to store , And read... In the corresponding way . Old Microsoft Word The document uses binary file format , And new docx The file extension is ZIP file .


WeChat official account
Dotnet Forum
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334595.html
边栏推荐
- 先中二叉建树
- Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
- Chapter VI project information management system summary
- TP5 email (2020-05-27)
- Load view Caton
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- ASP.NET和ASP.NETCore多环境配置对比
- [software testing] understand the basic knowledge of software testing
- MYSQL_ From mastery to abandonment
- [ncnn] - the meaning of - 23300 in param
猜你喜欢

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)

BLDC double closed loop (speed PI + current PI) Simulink simulation model

Xamarin效果第二十二篇之录音效果

准备一个月去参加ACM,是一种什么体验?

Er and eer models

Source Generator实战

最通俗易懂的依赖注入之生命周期
![Introduction to ACM [TSP problem]](/img/9f/4e3592542d989b2fbb6d82f7f2fbd2.png)
Introduction to ACM [TSP problem]

Opencv combines multiple pictures into video

Cherno_ Game engine series tutorial (5): 101~
随机推荐
Creating wechat voucher process with PHP
Opencv reads webcam video and saves it locally
Openfeign service call
Detailed log display of openfeign call
最通俗易懂的依赖注入之生命周期
Regular object type conversion tool - Common DOM class
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
ASP.NET 6 中间件系列 - 执行顺序
求二叉树的叶子结点个数
Openfeign timeout setting
How to deploy a website with only a server and no domain name?
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
再战leetcode (290.单词规律)
Plug in for vscode
使用栈来解决”迷你语法分析器“的问题
Use of MySQL command line client and common commands
Openfeign details show
AC & A2C & A3C
微软是如何解决 PC 端程序多开问题的——内部实现
Response processing of openfeign