fgetl - Read line from file, removing newline characters - MATLAB (original) (raw)

Main Content

Read line from file, removing newline characters

Syntax

Description

tline = fgetl([fileID](#d126e531458)) returns the next line of the specified file, removing the newline characters.

example

Examples

collapse all

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

collapse all

File identifier of an open file, specified as an integer. Before usingfgetl to read a line from the file, you must usefopen to open the file and obtain its identifier fileID.

Data Types: double

Tips

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced before R2006a

expand all

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.