Next: , Previous: Simple Output, Up: C-Style I/O Functions


14.2.3 Line-Oriented Input

To read from a file it must be opened for reading using fopen. Then a line can be read from the file using fgetl as the following code illustrates

     fid = fopen ("free.txt");
     txt = fgetl (fid)
          -| Free Software is needed for Free Science
     fclose (fid);

This of course assumes that the file ‘free.txt’ exists and contains the line ‘Free Software is needed for Free Science’.

— Built-in Function: fgetl (fid, len)

Read characters from a file, stopping after a newline, or EOF, or len characters have been read. The characters read, excluding the possible trailing newline, are returned as a string.

If len is omitted, fgetl reads until the next newline character.

If there are no more characters to read, fgetl returns −1.

See also: fread, fscanf.

— Built-in Function: fgets (fid)
— Built-in Function: fgets (fid, len)

Read characters from a file, stopping after a newline, or EOF, or len characters have been read. The characters read, including the possible trailing newline, are returned as a string.

If len is omitted, fgets reads until the next newline character.

If there are no more characters to read, fgets returns −1.

See also: fputs, fopen, fread, fscanf.

— Built-in Function: fskipl (fid, count)

Skip a given number of lines, i.e., discards characters until an end-of-line is met exactly count-times, or end-of-file occurs. Returns the number of lines skipped (end-of-line sequences encountered). If count is omitted, it defaults to 1. count may also be Inf, in which case lines are skipped to the end of file. This form is suitable for counting lines in a file.

See also: fgetl, fgets.