00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032 #include "../tester.h"
00033
00034 const char *test_stdio2(void)
00035 {
00036 FILE *file;
00037 const char *file_name = "/test";
00038
00039 TPRINTF("Open file \"%s\" for writing...", file_name);
00040 errno = 0;
00041 file = fopen(file_name, "wt");
00042 if (file == NULL) {
00043 TPRINTF("errno = %d\n", errno);
00044 return "Failed opening file";
00045 } else
00046 TPRINTF("OK\n");
00047
00048 TPRINTF("Write to file...");
00049 fprintf(file, "integer: %u, string: \"%s\"", 42, "Hello!");
00050 TPRINTF("OK\n");
00051
00052 TPRINTF("Close...");
00053 if (fclose(file) != 0) {
00054 TPRINTF("errno = %d\n", errno);
00055 return "Failed closing file";
00056 } else
00057 TPRINTF("OK\n");
00058
00059 TPRINTF("Open file \"%s\" for reading...", file_name);
00060 file = fopen(file_name, "rt");
00061 if (file == NULL) {
00062 TPRINTF("errno = %d\n", errno);
00063 return "Failed opening file";
00064 } else
00065 TPRINTF("OK\n");
00066
00067 TPRINTF("File contains:\n");
00068 while (true) {
00069 int c = fgetc(file);
00070 if (c == EOF)
00071 break;
00072 TPRINTF("%c", c);
00073 }
00074
00075 TPRINTF("\nClose...");
00076 if (fclose(file) != 0) {
00077 TPRINTF("errno = %d\n", errno);
00078 return "Failed closing file";
00079 } else
00080 TPRINTF("OK\n");
00081
00082 return NULL;
00083 }