fgetl

Reads the characters in a file until reaching request_length of characters read. Reads until a new line or end of file if request_length is not provided.

Syntax

fgetl(fileID)

fgetl(fileID, request_length)

[text, actual_length] = fgetl(fileID, request_length)

[text, actual_length] = fgetl(fileID)

Inputs

fileID
File ID returned from an earlier call to fopen(). stdin() cannot be used as a fileId. If it is required to get the input from stdin, input should be used.
Type: double | integer
request_length
Maximum number of characters to be read. Reading stops when a new line or end of file is encountered.
Type: double | integer

Outputs

text
Characters read from the file. fgetl() strips the trailing new line character from the text.
Type: double | integer
actual_length
Number of characters actually read from the file. This number does not include any new line character stripped by fgetl().
Type: double | integer

Example

% open file for writing
fileID = fopen('testfile', 'w+')
fprintf_result = fprintf(fileID, '%s\n', 'hello ')
fprintf_result = fprintf(fileID, '%s\n', 'world!')
fflush_result = fflush(fileID)
frewind_result = frewind(fileID)
% read the file back.  fgetl strips training new-line
while feof(fileID) == false
	printf('%s', fgetl(fileID))
end
printf('\n')
fclose_result = fclose(fileID)
fileID = 6
fprintf_result = 7
fprintf_result = 7
fflush_result = 0
frewind_result = 0
hello world!
fclose_result = 0