当前位置:网站首页>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
边栏推荐
- 由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
- Dynamic sequence table + OJ
- TP5 multi conditional where query (using PHP variables)
- Chapter IV project cost management of information system project manager summary
- MAUI初体验:爽
- 宁德时代地位不保?
- 再战leetcode (290.单词规律)
- Typescript Learning Guide
- tf. keras. layers. Timedistributed function
- Array and collection types passed by openfeign parameters
猜你喜欢
随机推荐
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
MAUI初体验:爽
使用DFS来解决“字典序排数”问题
使用两种方法来解决“最大回文数乘积”问题
How to write the expected salary on your resume to double your salary during the interview?
Systemctl start Prometheus + grafana environment
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
最通俗易懂的依赖注入之生命周期
Response processing of openfeign
宁德时代地位不保?
Close the computer port
[learn junit5 from official documents] [II] [writingtests] [learning notes]
Typescript Learning Guide
Tips in MATLAB
AspNetCore配置多环境log4net配置文件
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Traversée de l'arbre L2 - 006
Array and collection types passed by openfeign parameters
Processes and threads
TP5 where query one field is not equal to multiple values








