Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

fgetl

Read line from file, removing newline characters

Syntax

tline = fgetl(fileID)

Description

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

  • If the file is nonempty, then fgetl returns tline as a character vector.

  • If the file is empty and contains only the end-of-file marker, then fgetl returns tline as a numeric value -1.

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.

frewind(fid);

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.

length(line_ex)
ans = 19
length(line_in)
ans = 20

fgetl returns an output that displays in one line, while fgets returns an output that includes the newline character and, therefore, displays in two lines.

line_ex
line_ex = 
'Oranges and lemons,'
line_in 
line_in = 
    'Oranges and lemons,
     '

Close the file.

fclose(fid);

Input Arguments

collapse all

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

Data Types: double

Tips

  • fgetl reads characters using the encoding scheme associated with the file. To specify the encoding scheme, use fopen.

Introduced before R2006a

Was this topic helpful?