Loadand display the filereadline.cfor an example of reading a complete line. This program is very similar to those we have been studying except for the addition of a new quantity, the NULL.
#include "/sys/stdio.h"
main( )
{
FILE *fp1;
char oneword[100];
char *c;
fp1 = fopen("TENLINES.TXT","r");
do {
c = fgets(oneword,100,fp1); /* get one line from the file */
if (c != NULL);
printf("%s",oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
fclose(fp1);
}
We are using "fgets" which reads in an entire line, including the newline character into a buffer. The buffer to be read into is the first argument in the function call, and the maximum number of characters to read is the second argument, followed by the file pointer. This function will read characters into the input buffer until it either finds a newline character, or it reads the maximum number of characters allowed minus one. It leaves one character for the end of string NULL character. In addition, if it finds an EOF, it will return a value of NULL. In our example, when the EOF is found, the pointer "c" will be assigned the value of NULL. NULL is defined as zero in your "stdio.h" file.
When we find that "c" has been assigned the value of NULL, we can stop processing data, but we must check before we print just like in the last program. Last of course, we close the file.
#include "/sys/stdio.h"
main( )
{
FILE *fp1;
char oneword[100];
char *c;
fp1 = fopen("TENLINES.TXT","r");
do {
c = fgets(oneword,100,fp1); /* get one line from the file */
if (c != NULL);
printf("%s",oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
fclose(fp1);
}
We are using "fgets" which reads in an entire line, including the newline character into a buffer. The buffer to be read into is the first argument in the function call, and the maximum number of characters to read is the second argument, followed by the file pointer. This function will read characters into the input buffer until it either finds a newline character, or it reads the maximum number of characters allowed minus one. It leaves one character for the end of string NULL character. In addition, if it finds an EOF, it will return a value of NULL. In our example, when the EOF is found, the pointer "c" will be assigned the value of NULL. NULL is defined as zero in your "stdio.h" file.
When we find that "c" has been assigned the value of NULL, we can stop processing data, but we must check before we print just like in the last program. Last of course, we close the file.