Files
robert-cs50/c_programming/files/lines.c

37 lines
896 B
C

// A program that iterates one line at a time.
// uses fgets.
// https://manual.cs50.io/3/fgets
// read the lines from lines.txt and print them out.
#include <stdio.h>
// char *fgets(char s[restrict .size], int size, FILE *restrict stream);
// reads in at most one less than size characters from stream and stores them
// into the buffer pointed to by s. Reading stops after an EOF or a newline.
// If a newline is read, it is stored into the buffer. A terminating null byte
// ('\0') is stored after the last character in the buffer.
// lines.c is based on the example at https://en.cppreference.com/c/io/fgets
#define SIZE 64
int main(void)
{
FILE* infile = fopen("lines.txt", "r");
char buf[SIZE];
while (fgets(buf, sizeof buf, infile) != NULL)
{
printf("%s", buf);
}
if (feof(infile))
{
puts("End of file reached");
}
return 0;
}