Read line from file, keeping newline characters
tline
= fgets(fileID
)tline
= fgets(fileID
, nchar
)
reads
the next line of the specified file, including the newline characters. tline
= fgets(fileID
)fileID
is
an integer file identifier obtained from fopen
. tline
is
a character vector unless the line contains only the end-of-file marker.
In this case, tline
is the numeric value -1
.
fgets
reads characters using the encoding scheme
associated with the file. To specify the encoding scheme, use fopen
.
returns
at most tline
= fgets(fileID
, nchar
)nchar
characters of the next line. tline
does
not include any characters after the newline characters or the end-of-file
marker.
Read and display the file fgets.m
. Because fgets
keeps
newline characters and disp
adds a newline character,
this code displays the file with double-spacing:
fid = fopen('fgets.m'); tline = fgets(fid); while ischar(tline) disp(tline) tline = fgets(fid); end fclose(fid);
Compare these results to the fgetl
example,
which replaces the calls to fgets
with fgetl
.