if (from==NULL) perror ("Error opening file"); else { fseek (from, 0, SEEK_END); size=ftell (from); fclose (from); printf ("Size of myfile.txt: %ld bytes.\n",size); }
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) long getFileSize(const char *filename) { long size; FILE *from, *to; char ch; if(argc!=3) { printf("Usage: copy <source> <destination>\n"); exit(1); } /* open source file */ if((from = fopen(argv[1], "rb"))==NULL) { printf("Cannot open source file.\n"); exit(1); } /* open destination file */ if((to = fopen(argv[2], "wb"))==NULL) { printf("Cannot open destination file.\n"); exit(1); } /* copy the file */ while(!feof(from)) { ch = fgetc(from); if(ferror(from)) { printf("Error reading source file.\n"); exit(1); } if(!feof(from)) fputc(ch, to); if(ferror(to)) { printf("Error writing destination file.\n"); exit(1); } } if(fclose(from)==EOF) { printf("Error closing source file.\n"); exit(1); } if(fclose(to)==EOF) { printf("Error closing destination file.\n"); exit(1); } if (from==NULL) perror ("Error opening file"); else { fseek (from, 0, SEEK_END); size=ftell (from); fclose (from); printf ("Size of myfile=: %ld bytes.\n",size); } return 0; }
fseek
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)