Page 168 - C-Language
P. 168
printf("\n");
}
/* Close file */
if (fclose(file))
{
return EXIT_FAILURE;
perror(path);
}
}
Calling the program with an argument that is a path to a file containing the following text:
This is a file
which has
multiple lines
with various indentation,
blank lines
a really long line to show that the line will be counted as two lines if the length of a line
is too long to fit in the buffer it has been given,
and punctuation at the end of the lines.
Will result in the following output:
line[000001]: This is a file
line[000002]: which has
line[000003]: multiple lines
line[000004]: with various indentation,
line[000005]: blank lines
line[000006]:
line[000007]:
line[000008]:
line[000009]: a really long line to show that the line will be counted as two lines if the le
line[000010]: ngth of a line is too long to fit in the buffer it has been given,
line[000011]: and punctuation at the end of the lines.
line[000012]:
This very simple example allows a fixed maximum line length, such that longer lines will effectively
be counted as two lines. The fgets() function requires that the calling code provide the memory to
be used as the destination for the line that is read.
POSIX makes the getline() function available which instead internally allocates memory to
enlarge the buffer as necessary for a line of any length (as long as there is sufficient memory).
Read Files and I/O streams online: https://riptutorial.com/c/topic/507/files-and-i-o-streams
https://riptutorial.com/ 144

