#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str[80] = "This is a test.\n";
FILE *fp;
char *p;
int i;
/* open my.txt for output */
if((fp = fopen("my.txt", "w"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
/* write str to disk */
p = str;
while(*p) {
if(fputc(*p, fp)==EOF) {
printf("Error writing file.\n");
exit(1);
}
p++;
}
fclose(fp);
/* open my.txt for input */
if((fp = fopen("my.txt", "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
/* read back the file */
for(;;) {
i = fgetc(fp);
if(i == EOF) break;
putchar(i);
}
fclose(fp);
return 0;
}
|