Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. long int filesize (FILE * fp) {
  5.     long int save_pos, size_of_file;
  6.    
  7.     save_pos = ftell(fp);
  8.     fseek(fp, 0L, SEEK_END);
  9.     size_of_file = ftell(fp);
  10.     fseek(fp, save_pos, SEEK_SET);
  11.    
  12.     return(size_of_file);
  13. }
  14.  
  15. int main (int argc, char * argv[]) {
  16.     FILE * fin;
  17.     FILE * fout;
  18.    
  19.     fout = fopen("output.txt", "w");
  20.    
  21.     if (argv[1] == NULL) {
  22.         fprintf(fout, "Usage: stat filename\n");
  23.         fclose(fout);
  24.         exit(1);
  25.     } else {
  26.         fin = fopen(argv[1], "r");
  27.        
  28.         if (fin == NULL) {
  29.             fprintf(fout, "Can't open file %s", argv[1]);
  30.             fclose(fout);
  31.             exit(2);
  32.         } else {
  33.             fprintf(fout, "%ld", filesize(fin));
  34.             fclose(fout);
  35.             fclose(fin);
  36.             exit(0);
  37.         }
  38.     }
  39.    
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement