Main Content

fclose

Close one or all open files

Description

fclose(fileID) closes an open file.

example

fclose('all') closes all open files.

status = fclose(___) returns a status of 0 when the close operation is successful. Otherwise, it returns -1. You can use this syntax with any of the input arguments of the previous syntaxes.

Examples

collapse all

Open the badpoem.txt file, read the first line, and then close the file.

Use fopen to open the file. This function assigns a unique file id to use for reading and writing to the file.

fid = fopen('badpoem.txt')
fid = 
3

Read the first line from the file using fgetl.

fgetl(fid)
ans = 
'Oranges and lemons,'

Close the file.

fclose(fid);

Input Arguments

collapse all

File identifier of an open file, specified as an integer. Before closing a file with fclose, you must use fopen to open the file and obtain its identifier fileID.

Data Types: double

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

expand all