Added simple WCSA
[SXSI/TextCollection.git] / swcsa / utils / fileInfo.c
1
2 #include "fileInfo.h"
3
4 unsigned long fileSize (char *filename){
5         FILE *fpText;
6         unsigned long fsize;
7         fpText = fopen(filename,"rb");
8         fsize=0;
9         if (fpText) {
10                 fseek(fpText,0,2);
11                 fsize= ftell(fpText);
12                 fclose(fpText);
13                 ////fprintf(stderr,"fileSize = %ld",fsize);
14         }
15         return fsize;
16 }
17
18 /*copies from infile to outfile */
19 void copyFile (char *infile, char *outfile){
20         FILE *in, *out;
21         unsigned long fsize;
22         
23         if ( (in = fopen(infile,"rb")) <0) {
24                 printf("Cannot open file %s\n", infile); exit(0);
25         }       
26
27         unlink(outfile);
28         if( (out = fopen(outfile, "w")) < 0) {
29                 printf("Cannot open file %s\n", outfile);
30                 exit(0);
31         }       
32
33         fsize=fileSize(infile);
34         if (fsize) {
35                 char *buff = (char *) malloc(sizeof(char)*fsize);
36                 if (fread(buff,sizeof(char),fsize,in)) {
37                         fwrite(buff,sizeof(char),fsize,out);                    
38                 }               
39                 free(buff);
40         }                       
41         fclose(in);
42         fclose(out);    
43 }
44