binary files
时间: 2023-11-11 19:58:36 浏览: 26
二进制文件(binary files)是一种计算机文件形式,其中包含了非文本数据。与文本文件不同,二进制文件中的数据以二进制形式存储,可以包含各种格式的信息,例如图像、音频、视频等。
二进制文件与文本文件的主要区别在于数据的存储方式。文本文件以字符编码的形式存储数据,而二进制文件直接以二进制代码存储数据。这使得二进制文件更加高效,可以处理更复杂的数据结构。
与文本文件不同,二进制文件不能直接由人类读取和编辑。它们通常是由特定的应用程序或编码器生成和解析。因此,当我们需要读取或修改二进制文件时,需要使用适当的工具或编程语言来处理。
相关问题
fortran binary
Fortran is a programming language that can be used to write binary files. Binary files are files that contain non-textual data, such as images, audio, or executable code.
To write binary files in Fortran, you can use the "OPEN" statement with the "ACCESS='STREAM'" option. This allows you to read and write binary data to and from the file.
Here's an example program that writes binary data to a file:
```
program write_binary_file
implicit none
integer :: i, n
real :: x(100)
character(len=20) :: filename
integer, parameter :: file_unit = 10
! Set the file name
filename = 'data.bin'
! Open the file for writing
open(unit=file_unit, file=filename, access='stream', form='unformatted', status='replace')
! Write some data to the file
n = 100
do i = 1, n
x(i) = i**2
end do
write(file_unit) n, x(1:n)
! Close the file
close(file_unit)
end program write_binary_file
```
This program writes an array of 100 real numbers to a binary file named "data.bin". The first value written to the file is the number of elements in the array, followed by the array itself. The "form='unformatted'" option tells Fortran to write the data in binary format.
To read binary files in Fortran, you can use the same "OPEN" statement with the "ACCESS='STREAM'" option, and then use the "READ" statement to read the data from the file.
翻译一下function format = stlGetFormat(fileName) %STLGETFORMAT identifies the format of the STL file and returns 'binary' or %'ascii' fid = fopen(fileName); % Check the file size first, since binary files MUST have a size of 84+(50*n) fseek(fid,0,1); % Go to the end of the file fidSIZE = ftell(fid); % Check the size of the file if rem(fidSIZE-84,50) > 0 format = 'ascii'; else % Files with a size of 84+(50*n), might be either ascii or binary... % Read first 80 characters of the file. % For an ASCII file, the data should begin immediately (give or take a few % blank lines or spaces) and the first word must be 'solid'. % For a binary file, the first 80 characters contains the header. % It is bad practice to begin the header of a binary file with the word % 'solid', so it can be used to identify whether the file is ASCII or % binary. fseek(fid,0,-1); % go to the beginning of the file header = strtrim(char(fread(fid,80,'uchar')')); % trim leading and trailing spaces isSolid = strcmp(header(1:min(5,length(header))),'solid'); % take first 5 char fseek(fid,-80,1); % go to the end of the file minus 80 characters tail = char(fread(fid,80,'uchar')'); isEndSolid = findstr(tail,'endsolid'); % Double check by reading the last 80 characters of the file. % For an ASCII file, the data should end (give or take a few % blank lines or spaces) with 'endsolid <object_name>'. % If the last 80 characters contains the word 'endsolid' then this % confirms that the file is indeed ASCII. if isSolid & isEndSolid format = 'ascii'; else format = 'binary'; end end fclose(fid);
这段代码的作用是识别STL文件的格式并返回'binary'或'ascii'。首先打开文件并检查文件大小,如果文件大小为84+(50*n),则该文件可能是ASCII或二进制格式。接着读取文件的前80个字符,并检查第一个单词是否为'solid'。如果是,则该文件为ASCII格式;否则,该文件为二进制格式。最后关闭文件并返回格式。