File Handling in C:- The below example will show you how you can read the data from file and write the data in file using C language.
file.c
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define true 1
main()
{
FILE *fptr;
char ch;
clrscr();
    //Write the File Charchter by Charchter
    fptr=fopen("MYFILE.txt","w");
    if(fptr==NULL)
    {
        printf("cant create file");
        getch();
        exit(1);
    }
    printf("enter the text:=");
        while(true)
        {
            ch=getchar();
            if(ch=='!')
            break;
            fputc(ch,fptr);
        }
fclose(fptr);
       //Read the File Charchter by Charchter
    fptr=fopen("MYFILE.txt","r");
    if(fptr==NULL)
    {
        printf("cant read the file");
        getch();
        exit(1);
    }
        while(true)
        {
            if(feof(fptr))
            break;
            printf("%c",fgetc(fptr));
        }
fclose(fptr);
getch();
}
                       
                    
0 Comment(s)