15.File Handling
/-------------------------------------------/
(1)
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("data","r");
int cw=0,i=0,cd=0,cl=0;;
char s[20],ch;
while(fscanf(fp,"%s",s)!=EOF)
{
for(i=0;s[i];i++)
{
if(s[i] >= '0' && s[i] <= '9')
cd++;
}
cw++;
}
rewind(fp);
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
cl++;
}
printf(" Number of words : %d \n",cw);
printf(" Number of digits : %d \n",cd);
printf(" Number of line : %d \n",cl);
}
/-------------------------------------------/
(2)
#include<stdio.h>
void main()
{
FILE *fp,*tp;
fp=fopen("data","r+");
char a[100],ch;
int i;
while((ch=fgetc(fp))!=EOF)
{
fseek(fp,-1,SEEK_CUR);
if(ch>='a' && ch<='z')
ch=ch-32;
fputc(ch,fp);
a[i++]=ch;
}
fclose(fp);
}
/-------------------------------------------/
(3)
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("data","r");
int cl=0,i=0;
char ch;
while((ch=fgetc(fp))!=EOF)
{
cl++;
if(ch==' ' || ch=='\n')
{
printf("%d\n",cl-1);
cl=0;
}
}
}
/-------------------------------------------/
(4)
#include<stdio.h>
void main(int argc,char **argv)
{
FILE *sf1,*sf2,*df;
sf1=fopen(argv[1],"r");
sf2=fopen(argv[2],"r");
df=fopen(argv[3],"w+");
char ch;
while((ch=fgetc(sf1))!=EOF)
{
fputc(ch,df);
fseek(df,+1,SEEK_CUR);
}
rewind(df);
fseek(df,+1,SEEK_CUR);
while((ch=fgetc(sf2))!=EOF)
{
fputc(ch,df);
fseek(df,+1,SEEK_CUR);
}
}
/-------------------------------------------/
(5)
#include<stdio.h>
#include<string.h>
void main(int argc,char **argv)
{
FILE *sf1,*sf2,*df;
sf1=fopen(argv[1],"r");
sf2=fopen(argv[2],"r");
df=fopen(argv[3],"w+");
char s1[50],s2[50];
int c=0;
while(fscanf(sf1,"%s",s1)!=EOF)
{
fprintf(df,"%s ",s1);
fscanf(sf2,"%s",s2);
fprintf(df,"%s ",s2);
}
}
/-------------------------------------------/
(6)
#include<stdio.h>
void main(int argc,char **argv)
{
FILE *fp1,*fp2,*df;
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"r");
df=fopen(argv[3],"w+");
char a[50],ch;
int nol1=0,nol2=0,c=0,noc1=0,noc2=0,counter=0,d=0;
while((ch=fgetc(fp1))!=EOF)
{
counter++;
if(ch=='\n')
{
nol1++;
if(counter>noc1)
noc1=counter;
counter=0;
}
}
counter=0;
while((ch=fgetc(fp2))!=EOF)
{
counter++;
if(ch=='\n')
{
nol2++;
if(counter>noc2)
noc2=counter;
counter=0;
}
}
if(nol2<nol1)
c=nol1;
else
c=nol2;
if(noc2<noc1)
d=noc1;
else
d=noc2;
rewind(fp1);
rewind(fp2);
while(c)
{
fgets(a,d+1,fp1);
fprintf(df,"%s",a);
fgets(a,d+1,fp2);
fprintf(df,"%s",a);
c--;
}
}
/-------------------------------------------/
(7)
#include<stdio.h>
#include<stdlib.h>
int nol=0,counter=0,noc=0,line=0;
char ch;
void main(int argc,char **argv)
{
FILE *fp,*df;
fp=fopen(argv[1],"r+");
df=fopen("data1.txt","w+");
line=atoi(argv[2]);
while((ch=fgetc(fp))!=EOF)
{
counter++;
if(ch=='\n')
{
nol++;
if(counter>noc)
noc=counter;
counter=0;
}
}
rewind(fp);
char arr[noc+1];
counter=0;
while(fgets(arr,noc+1,fp))
{
counter++;
if(line==counter)
continue;
fputs(arr,df);
}
fclose(fp);
fclose(df);
fp=fopen(argv[1],"w+");
df=fopen("data1.txt","r+");
while(fgets(arr,noc+1,df))
fputs(arr,fp);
fclose(fp);
}
/-------------------------------------------/
(8)
#include<stdio.h>
#include<stdlib.h>
int nol=0,counter=0,noc=0,line=0;
char ch;
void main(int argc,char **argv)
{
FILE *fp,*df;
fp=fopen(argv[1],"r+");
df=fopen("data1.txt","w+");
while((ch=fgetc(fp))!=EOF)
{
counter++;
if(ch=='\n')
{
nol++;
if(counter>noc)
noc=counter;
counter=0;
}
}
rewind(fp);
char arr[noc+1];
counter=0;
while(fgets(arr,noc+1,fp))
{
counter++;
if(counter==nol)
continue;
if(counter==1)
continue;
fputs(arr,df);
}
fclose(fp);
fclose(df);
fp=fopen(argv[1],"w+");
df=fopen("data1.txt","r+");
while(fgets(arr,noc+1,df))
fputs(arr,fp);
fclose(fp);
remove("data1.txt");
}
/-------------------------------------------/
(9)
#include<stdio.h>
#include<string.h>
FILE *fp;
void main(int argc,char **argv)
{
///////////////////////////////////////////////////////
fp=fopen(argv[1],"r+");
char a1;
int c1;
while((a1=fgetc(fp))!=EOF)
c1++;
char arr1[c1];
rewind(fp);
int i1=0;
while((a1=fgetc(fp))!=EOF)
{
arr1[i1]=a1;
i1++;
}
arr1[i1]='\0';
fclose(fp);
/////////////////////////////////////////////////////
char m[i1],s[10],z[10];
strcpy(m,arr1);
strcpy(s,argv[2]);
strcpy(z,argv[3]);
char temp[i1];
int i,c=0,len,len1,len2,p,q,flag=0,u=0,v;
for(len=0;m[len];len++);
for(len1=0;s[len1];len1++);
for(len2=0;z[len2];len2++);
for(i=0;i<len;i++)
{
if(s[0]==m[i])
{
for(p=0,q=i;s[p];p++,q++)
{
if(s[p]==m[q])
flag=1;
else
flag=0;
}
if(flag==1)
{
for(v=0;v<len2;u++,v++)
{
temp[u]=z[v];
}
i=i+len1;
}
flag=0;
}
temp[u]=m[i];
u++;
}
for(i=0;temp[i];i++)
{
m[i]=temp[i];
}
////////////////////////////////////////////
fp=fopen(argv[1],"w+");
fputs(m,fp);
fclose(fp);
}
/-------------------------------------------/
(10)
#include<stdio.h>
#include<string.h>
FILE *fp;
void main(int argc,char **argv)
{
///////////////////////////////////////////////////////
fp=fopen(argv[1],"r+");
char a1;
int c1;
while((a1=fgetc(fp))!=EOF)
c1++;
char arr1[c1];
rewind(fp);
int i1=0;
while((a1=fgetc(fp))!=EOF)
{
arr1[i1]=a1;
i1++;
}
arr1[i1]='\0';
fclose(fp);
/////////////////////////////////////////////
int i3;
int len=0;
for(len;arr1[len];len++);
printf("%d\n",len);
for(i3=0;arr1[i3];i3++)
{
if(i3==len)
break;
if(arr1[i3]==' ')
{
arr1[i3+1]=arr1[i3+1]-32;
arr1[i3-1]=arr1[i3-1]-32;
}
else if(arr1[i3]=='\n')
{
arr1[i3+1]=arr1[i3+1]-32;
arr1[i3-1]=arr1[i3-1]-32;
}
else if(i3==0)
{
arr1[i3]=arr1[i3]-32;
}
}
arr1[i3]='\0';
////////////////////////////////////////////
printf("%s",arr1);
fp=fopen(argv[1],"w+");
fputs(arr1,fp);
fclose(fp);
}
/-------------------------------------------/
Comments
Post a Comment