Files Prs
Files Prs
/* Program to create a new file(w mode) & write(fputc()) the contents onto the file */
#include<stdio.h>
Int main()
FILE *fp;
char ch;
if(fp==NULL)
while(1)
if(ch==EOF)
break;
2. /*Program to read the contents from a file and display on the screen */
#include<stdio.h>
Int main()
{
FILE *fp;
char ch;
fp=fopen("xyz.txt","r");
if(fp==NULL)
while(1)
ch=fgetc(fp);
if(ch==EOF)
break;
putchar(ch);
fclose(fp);
#include<stdio.h>
Int main()
FILE *fp;
char fname[20],ch;
gets(fname);
fp=fopen(fname,"r");
if(fp==NULL)
fclose(fp);
unlink(fname);
fp=fopen(fname,"r");
if(fp==NULL)
fclose(fp);
return 0;
#include<stdio.h>
#define size 10
Int main()
while(fgets(str,size,fr)!=NULL)
fclose(fr);
fclose(fw);
if(fp!=NULL)
char ch=fgetc(fp);
while(ch!=EOF)
printf("%c",ch);
ch=fgetc(fp);
}
return 0;
5. /*file merge*/
#include<stdio.h>
Int main()
{ char c;
}
6. //program to demonstrate fseek()
#include<stdio.h>
int main()
FILE *fp;
fp=fopen("demo.txt","w+");
fputs("computers",fp);
fseek(fp,9,SEEK_SET);
fputs("science",fp);
fclose(fp);
return 0;
#include<stdio.h>
main()
FILE *fp;
fp=fopen("pps.txt","w+");
fseek(fp,0,SEEK_END);//it will bring the pointer to end of the file content with 0 distance*/
printf("%d",ftell(fp));
fclose(fp);
#include<stdio.h>
main()
FILE *fp;
fp=fopen("mid.txt","w+");
int a;
float b;
char ch[20];
printf("enter data");
scanf("%d%f%s",&a,&b,ch);
fprintf(fp,"%d%f%s",a,b,ch);
rewind(fp);
fscanf(fp,"%d%f%s",&a,&b,ch);
printf("%d%f%s",a,b,ch);
fclose(fp);