dos_compilers/Borland Turbo C++ v1/EXAMPLES/INTRO34.C
2024-07-02 07:34:51 -07:00

40 lines
967 B
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* INTRO34.C--Example from Chapter 4 of User's Guide */
#include <stdio.h>
#include <stdlib.h>
FILE *textfile; /* Pointer to file being used */
char line[81]; /* Char array to hold lines read from file */
int main()
{
/* Open file, testing for success */
if ((textfile = fopen("intro34.txt", "w")) == NULL) {
printf("Error opening text file for writing\n");
exit(0);
}
/* Write some text to the file */
fprintf(textfile, "%s\n", "one");
fprintf(textfile, "%s\n", "two");
fprintf(textfile, "%s\n", "three");
/* Close the file */
fclose(textfile);
/* Open file again */
if ((textfile = fopen("intro34.txt", "r")) == NULL) {
printf("Error opening text file for reading\n");
exit(0);
}
/* Read file contents */
while ((fscanf(textfile, "%s", line) != EOF))
printf("%s\n", line);
/* Close file */
fclose(textfile);
return 0;
}