#include <stdio.h>
int main(void) {
// Open a file for writing
FILE *fp = fopen("test.txt", "w");
// Write some text to the file
fprintf(fp, "Hello, world!\n");
fprintf(fp, "This is a test.\n");
// Close the file
fclose(fp);
// Open the file for reading
fp = fopen("test.txt", "r");
// Read and print the contents of the file
char buffer[100];
while (fgets(buffer, sizeof buffer, fp) != NULL) {
fputs(buffer, stdout);
}
// Close the file
fclose(fp);
return 0;
}
This program opens a file called test.txt
for writing and writes some text to it. It then closes the file and reopens it for reading. It reads the contents of the file line by line and prints them to the standard output. Finally, it closes the file.
Here is an example of the output of this program:
Hello, world!
This is a test.
0 Comments