fgets
Read string from a file, stopping after a newline, or EOF, or n characters have been read.
📝Syntax
res = fgets(f)
res = fgets(f, n)
📥Input Arguments
Parameter Description
f a file descriptor
n a scalar: number of characters
📤Output Arguments
Parameter Description
res a string or -1
📄Description

Read string from a file, stopping after a newline, or EOF, or n characters have been read.

If there is no more character to read, fgets will return -1.

If n is omitted, fgets reads until the next newline.

characters encoding uses fopen parameter.

💡Examples
  fid = fopen([nelsonroot(), '/etc/startup.m']);
  tline = fgets(fid);
  while ischar(tline)
  disp(tline)
  tline = fgets(fid);
  end

  fclose(fid);
fid = fopen([nelsonroot(), '/etc/startup.m']);

  tline = fgets(fid, 5);
  while ischar(tline)
  disp(tline)
  tline = fgets(fid, 5);
  end

  fclose(fid);
🔗See Also
fclosefopenfgetl
🕔Version History
Version Description
1.0.0 initial version
Edit this page on GitHub