Open file for read/write operation in binary mode

# include <stdio.h> # include <conio.h> # include <process.h>   void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.dat","wb"); if (fp==NULL) { printf("Can not open file"); exit(1); } printf("Press ‘.’ to exit:"); while(c!=’.’) { c=getche(); fputc(c,fp); } fclose(fp); fp=fopen("mydata.dat","rb");   printf("\n Contents read :"); while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Open a file in append mode in C

# include <stdio.h> # include <conio.h> # include <process.h>   void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.txt","a+"); if(fp==NULL) { printf("Can not open file"); exit(1); } printf("Press ‘.’ to exit:"); while(c!=’.’) { c=getche(); fputc(c,fp); } printf("\n Contents read :"); rewind(fp); while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Use w+ mode for writing and reading a file in C

# include <stdio.h> # include <conio.h> # include <process.h> void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.txt","w+"); if(fp==NULL) { printf("Can not open file"); exit(1); } printf("press ‘.’ to exit :"); while(c!=’.’) { c=getche(); fputc(c,fp); } rewind(fp); printf("\n Contents read :");   while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Open pre-existing file and append string to file in C

# include <stdio.h> # include <conio.h> # include <process.h> void main() { FILE *fp; char c; clrscr();   printf("File before appending :\n");   fp=fopen("mydata.txt","r"); while(!feof(fp)) { c=fgetc(fp); printf("%c",c); } fp=fopen("mydata.txt","a");   if(fp==NULL) { printf("File can not appended"); exit(1); } printf("\n Enter string to append :");   while(c!=’.’) { c=getche(); fputc(c,fp); } fclose(fp);   printf("\n File […]
Continue reading…

Enjoyed this post? Share it!

 
 

program to write data to text file and read in C

# include <stdio.h> # include <conio.h> # include <process.h>   void main() { FILE *fp; char c=’ ‘; clrscr(); fp=fopen("mydata.txt","w"); if(fp==NULL) { printf("Can not open file mydata.txt"); exit(1); } printf("Write data, press ‘-‘ to exit:"); while(c!=’-‘) { c=getche(); fputc(c,fp); } fclose(fp); printf("\n Contents read :"); fp=fopen("mydata.txt","r"); while(!feof(fp)) printf("%c",getc(fp)); }
Continue reading…

Enjoyed this post? Share it!

 
 

Retrieve user specified lines from a text & store these lines to another destination file

#include <stdio.h> #include <process.h>   #define MAX 500 void makendx(FILE *rfp, FILE *nfp) { long line=0, spot=0; int ch;   /* leave space for storing total number of lines in file*/ fseek(nfp,(long)sizeof(long),0);   /* Write the position of the first line*/ fwrite((char*)&spot, sizeof(long),1,nfp);   /* loop to store the positions of the subsequent lines */ […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program listing for simulating the cat command in UNIX with error handling

#include <stdio.h> #include <process.h>   void copyfile(FILE *,FILE *);   int main(int argc, char *argv[]) { FILE *fileptr, *fopen();   if(argc>1) while(–argc>0) if((fileptr=fopen(*++argv, "r"))==NULL) { fprintf(stderr,"cat : can’t open %sn",*argv); exit(1); } else{ copyfile(fileptr,stdout); fclose(fileptr); } else copyfile(stdin, stdout); if(ferror(stdout)) { fprintf(stderr, "cat : error in writing at stdoutn" ); exit(2); } return(0); } /* […]
Continue reading…

Enjoyed this post? Share it!

 
 

Program listing for simulating the cat command in UNIX

#include <stdio.h> #include <process.h>   void copyfile(FILE *, FILE *);   int main(int argc,char *argv[]) { FILE *fileptr,*fopen(); if(argc>1) while(–argc>0) if((fileptr=fopen(*++argv, "r"))==NULL) { printf("cat : can’t open %sn",*argv); exit(1); } else { copyfile(fileptr,stdout); fclose(fileptr); } else copyfile(stdin, stdout); return(0); }   /* Function to copy file from rfp to wfp */ void copyfile (FILE *rfp,FILE […]
Continue reading…

Enjoyed this post? Share it!

 
 

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 […]
Continue reading…

Enjoyed this post? Share it!

 
 

Modes for Operating a File in C

Mode Description “r” Opens a file for reading “w” Creates a file for writing or truncate to zero length “a” Opens a file for writing at end or create for writing “r+” Opens an existing file for reading/writing “w+” Truncates to zero length or create file for updating “a+” Opens for updating at end “rb” […]
Continue reading…

Enjoyed this post? Share it!