How to write a C Program Writing a character into a file,reading the character from the console and writing the next letter into the file in C Programming Language ?
Solution:
/* Writing a character into a file,reading the character from the console and writing the next letter into the file*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp; //creates a file pointer
char ch;
int con;
fp=fopen("filehandling.txt","w+"); // creates a text file with label "filehandling" in write mode
if(fp==NULL) //checks for the filename validity
{
printf("\nUnable to open the file\n");
exit(0);
}
printf("\nFile opened successfully\n");
printf("\nEnter the character as input\n");
scanf("%c",&ch);
putc(ch,fp);
fclose(fp);
fp=fopen("filehandling.txt","rwa+");
ch=getc(fp);
con=ch;
con++;
if(con>=97 && con<122)
{
fprintf(fp,"\nthe next char is %c\n",con);
}
else
{
fprintf(fp,"\nthe next char is a\n");
fclose(fp);
// open filehandling.txt file and check the input and output given...
}
}