当前位置:网站首页>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
边栏推荐
- The difference between encodeuri and encodeuricomponent
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- 利用栈的回溯来解决“文件的最长绝对路径”问题
- 使用两种方法来解决“最大回文数乘积”问题
- 交换二叉树中每个结点的左和右
- Openfeign timeout setting
- Ningde's position in the times is not guaranteed?
- Sonic cloud real machine tutorial
- C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
猜你喜欢
最通俗易懂的依赖注入之生命周期
TP5 customization in extend directory succeeded and failed. Return information
[format] simple output (2)
REINFORCE
Detailed explanation of distributed things
Er and eer models
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Niuke white moon race 6 [solution]
Cherno_ Game engine series tutorial (5): 101~
随机推荐
TP5 multi conditional where query (using PHP variables)
Laravel's own paging query
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
FileNotFoundError: [Errno 2] No such file or directory
ASP.NET和ASP.NETCore多环境配置对比
Opencv fills the rectangle with a transparent color
[format] simple output (2)
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
Blazor University (12)组件 — 组件生命周期
tf. keras. layers. Embedding function
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Opencv combines multiple pictures into video
Laravel8- use JWT
TP5 inherits base and uses the variables in base
[Euler plan question 13] sum of large numbers
.Net Core 限流控制-AspNetCoreRateLimit
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
tf. keras. layers. Inputlayer function
eventBus
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统