fgets - Read line from file, keeping newline characters - MATLAB (original) (raw)
Main Content
Read line from file, keeping newline characters
Syntax
Description
[tline](#mw%5Fa548458f-7ca8-49ce-8103-5c934b2d3dd3) = fgets([fileID](#d126e531939))
reads the next line of the specified file, including the newline characters.
[tline](#mw%5Fa548458f-7ca8-49ce-8103-5c934b2d3dd3) = fgets([fileID](#d126e531939),[nchar](#d126e531971))
returns up to nchar
characters of the next line.
[[tline](#mw%5Fa548458f-7ca8-49ce-8103-5c934b2d3dd3),[ltout](#mw%5Ff5c42084-9939-4b59-87b8-a50a1a5a37e2)] = fgets(___)
also returns the line terminators, if any, in ltout
.
Examples
Read a single line from a file, first excluding newline characters, and then including them. Use the following file.
To read the first line from the file badpoem.txt
, use fopen
to open the file. Then read the first line using fgetl
, which excludes the newline character.
fid = fopen('badpoem.txt'); line_ex = fgetl(fid) % read line excluding newline character
line_ex = 'Oranges and lemons,'
To reread the same line from the file, first reset the read position indicator back to the beginning of the file.
Use the fgets function to read the first line from the file badpoem.txt
, which reads the line including the newline character.
line_in = fgets(fid) % read line including newline character
line_in = 'Oranges and lemons, '
Compare the output by examining the lengths of the lines returned by the fgetl
and fgets
functions.
fgetl
returns an output that displays in one line, while fgets
returns an output that includes the newline character and, therefore, displays it in two lines.
line_ex = 'Oranges and lemons,'
line_in = 'Oranges and lemons, '
Close the file.
Input Arguments
File identifier of an open file, specified as an integer. Before usingfgets
to read a line from the file, you must usefopen to open the file and obtain its identifier fileID
.
Data Types: double
Number of characters to read from the next line, specified as an integer.fgets
returns at most nchar
characters of the next line. If the number of characters specified bynchar
includes characters beyond the newline character or the end-of-file marker, then fgets
does not return any characters beyond the new line character or the end-of-file marker.
Data Types: double
Output Arguments
Next line in file, returned as a character vector or numeric scalar.
- If the file is nonempty, then
fgets
returnstline
as a character vector. - If the file is empty and contains only the end-of-file marker, then
fgets
returnstline
as a numeric value-1
.
Line terminators, returned as an integer.
The integers from 0
to 65535
correspond to Unicode®characters. You can convert integers to their corresponding Unicode representations using the char
function.
Tips
tline
does not include any characters after the newline characters or the end-of-file marker.fgets
reads characters using the encoding scheme associated with the file. To specify the encoding scheme, usefopen
.- To read lines from a file while removing newline characters, use fgetl.
Extended Capabilities
Usage notes and limitations:
- If the function
fgets
does not read content from the file, then the generated code reports either an error or returns an empty value instead of returning -1. - If the function
fgets
reads a null byte, the returned values might be truncated.
Version History
Introduced before R2006a
You can read data from primary online sources by performing low-level file read operations over an internet URL.
This function supports thread-based environments.