Current location - Quotes Website - Signature design - Usage of fread function
Usage of fread function

The fread function can read binary data from a file

Syntax:

A = fread(fid, count)

A = fread(fid, count, precision)

Where fid is the current position in the file pointed by the pointer, count refers to the number of data read, and precision indicates the format of the data type in which the data is read.

Example:

fid = fopen('alphabet.txt', 'r');

c = fread(fid, 5)'

c =

65 66 67 68 69

fclose(fid);

Program description: 26 English characters are stored in order in the alphabet file Letters, you must open the file before reading the file. Since the type of data to be read is not specified, the program specifies the default type as unsigned character type, which is uchar. 65, 66, 67, 68, and 69 represent A, B, C, and D. , E. When you finish using the file, remember to close the file to release the pointer.

Extended information:

Usage

int feof(FILE *stream);

Parameters

Stream: Pointer to the FILE structure?

Note: feof determines the end of the file by returning an error from the fread/fscanf function, so determining whether the file ends should be determined after reading the function. For example, when reading a file in a while loop, if the judgment is made before the reading function, if the last line of the file is a blank line, a memory error may occur.

Reference materials:

fread--Baidu Encyclopedia