A File read-write example in C

#include <stdio.h>
 #include <process.h>
 
 int main()
 {
    FILE *rfp,*wfp;
    char buff[80];
    int lineno=0;
 
    /*Open input and output files*/
    if((rfp=fopen("inpfile.c","r"))==NULL)
    {
        printf("Cannot open inpfile.c for reading !n");
        exit(1);
    }
    if((wfp=fopen("outfile.c","w"))==NULL)
    {
        printf("Cannot open outfile.c for writing !n");
        exit(2);
    }
 
    /*File read and write*/
    while(fgets(buff,80,rfp)!= NULL)
    {
        fprintf(wfp, "%4d:",++lineno);
        fputs(buff,wfp);
    }
    fclose(rfp);
    fclose(wfp);
    return 0;
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.