Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Read and Write the File in C ( File Handling )

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 328
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: