Reads a line of text from a file, retaining the newline (if present).
fh (scalar) – file handle of a file opened with
maxsize (scalar) – maximum size of string to read in, including the terminating null byte.
str (string) – Contains the text read from the file line specified by the file handle fh. The maximum size of the str, including the terminating null byte, is maxsize.
// Specify file name with full path fname = getGAUSSHome() $+ "examples/housing.csv"; // Open file handle for reading fh = fopen(fname, "r"); // Read the first line of the file // (up to 100 characters) s = fgets(fh, 100);
After the above code, s will equal:
// Read the second line of the file // by running 'fgets' again with the // same file handle. s = fgets(fh, 100);
After running the line above, s will be equal to:
fgets() procedure reads text from a file into a string. It reads up to a newline,
the end of the file, or \(maxsize-1\) characters. The result is placed in
str, which is then terminated with a null byte. The newline, if present,
If the file is already at end-of-file when you call
fgets(), your program
will terminate with an error. Use
eof() in conjunction with
fgets() to avoid
If the file was opened for update (see
fopen()) and you are switching from
writing to reading, don’t forget to call
fflush() first, to flush
the file’s buffer.
If you pass
fgets() the handle of a file opened with open (i.e., a data
set or matrix file), your program will terminate with a fatal error.