#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define LENGTH 81
int main()
{
char mystr[LENGTH];
int mychar=0;
FILE *pfile=NULL;
char *filename="myfile.txt";
printf("Enter an interesting string of up to %d characters:\n",LENGTH-1);
if(!fgets(mystr,LENGTH,stdin))
{
printf("Input from keyboard failed.\n");
exit(1);
}
// Creat a new file we can wrrite
if(fopen(filename,"w"))
{
printf("Error opening %s for writing.Program terminated.\n",filename);
exit(1);
}
setvbuf(pfile,NULL,_IOFBF,BUFSIZ);
for(int i=strnlen(mystr,LENGTH)-1;i>=0;--i)
{
fputc(mystr[i],pfile);
}
fclose(pfile);
// Open the file for read
if(fopen(filename,"r"))
{
printf("Error opening %s for reading .Program terminated.",filename);
exit(1);
}
setvbuf(pfile,NULL,_IOFBF,BUFSIZ);
// Read a charcaters from the file and display it
printf("the data read from the file is;\n");
while((mychar=fgetc(pfile))!=EOF)
{
putchar(mychar);
}
fclose(pfile);
pfile=NULL;
remove(filename);
return 0;
}