Saturday, 27 September 2014

PL Assignment 5 : File operations using command line argument


1:  /*  
2:   ==============================================================  
3:   Name    : File Operations  
4:   Author   : Mujahid, Shivkumar  
5:   Version   :  
6:   Copyright  : Your copyright notice  
7:   Description : Hello World in C, Ansi-style  
8:   ===========================================================  
9:   */  
10:  #include<stdio.h>  
11:  #include<stdlib.h>  
12:  void filecpy(char *,char *);  
13:  int main(int argc, char *argv[])  
14:  {  
15:       if(argc!=3)  
16:       {     printf("invalid number of arguments");  
17:            exit(0);  
18:       }  
19:       filecpy(argv[1],argv[2]);  
20:       return 0;  
21:  }  
22:  void filecpy(char *p,char *q)  
23:  {     FILE *f1,*f2;  
24:       char c[200];  
25:       f1=fopen(p,"r");  
26:       f2=fopen(q,"w");  
27:       if(f1==NULL)  
28:       {     printf("no such file");  
29:            exit(0);  
30:       }  
31:       while(fgets(c,200,f1)!=NULL)  
32:            fputs(c,f2);  
33:       fclose(f1);  
34:       fclose(f2);  
35:  }  
36:  -------------OUTPUT--------------  
37:  After Execution Contents of files are :  
38:  Source.txt  
39:  We are pictians.  
40:  Destination.txt  
41:  We are pictians.  
42:  #include<stdio.h>  
43:  #include<stdlib.h>  
44:  void count(char *);  
45:  int main(void)  
46:  {     char path[100];  
47:       printf("enter path of the file to be read");  
48:       scanf("%s",path);  
49:       count(path);  
50:       return 0;  
51:  }  
52:  void count(char *p)  
53:  {     FILE *f1,*f2;  
54:       int c=0,w=1,s=0,n=0;  
55:       char ch;  
56:       f1=fopen(p,"r");  
57:            if(f1==NULL)  
58:            {     printf("\nInvalid path");  
59:                 exit(0);  
60:            }  
61:            ch=fgetc(f1);  
62:            while(ch!=EOF)  
63:            {     c++;  
64:                 if(ch=='.')  
65:                      n++;  
66:                 if (ch==32)  
67:                 {     s++;  
68:                      w++;  
69:                 }  
70:                 if(ch=='\n')  
71:                      w++;  
72:                 ch=fgetc(f1);  
73:            }  
74:            f2=fopen("data.txt","w");  
75:            fprintf(f2,"Number of characters: %d",c);  
76:            fprintf(f2,"\nNumber of space: %d",s);  
77:            fprintf(f2,"\nNumber of sentences: %d",n);  
78:            fprintf(f2,"\nNumber of words: %d",w);  
79:            fclose(f1);  
80:            printf("\nAll the counts written to data.txt in current directory");  
81:  }  
82:  --------OUTPUT--------  
83:  After execution contents of files are:  
84:  Source.txt  
85:  We are pictians.  
86:  Destination.txt  
87:  Number of characters: 17  
88:  Number of space: 2  
89:  Number of sentences: 1  
90:  Number of words: 4 
 
https://drive.google.com/file/d/0BzbuE6BfgoRbYWRsWUdIMnlOS3M/edit?usp=sharing
 
 

No comments:

Post a Comment

Ads Inside Post