By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

binary file length

P: 1
i have creating abinary file but iwant to know how to calculate/know the file length because ihad error when i used filelength(f1 )for example?please help me
Nov 3 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
i have creating abinary file but iwant to know how to calculate/know the file length because ihad error when i used filelength(f1 )for example?please help me
you can get the file length from the file status, e.g.
Expand|Select|Wrap|Line Numbers
  1. // read file status, print size, dates etc
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. int main()
  7. {
  8. struct stat file_stat;
  9. int         status;
  10. char filename[50]={0};
  11. printf("enter filename ? ");
  12. scanf("%s", filename);
  13. int fildes = open(filename, O_RDWR);             // open file
  14.  
  15. // get file status for details see
  16. // http://www.opengroup.org/onlinepubs/007908799/xsh/fstat.html
  17. status = fstat(fildes, &file_stat);                 // get its status
  18.  
  19. // print file status informations for details see
  20. //   http://www.opengroup.org/onlinepubs/007908799/xsh/sysstat.h.html
  21. printf("file %s size %d\n", filename, file_stat.st_size);        // print status
  22. printf("last access %s\n", ctime(&file_stat.st_atime));
  23. printf("last mofification %s\n", ctime(&file_stat.st_mtime));
  24. printf("last status change %s\n", ctime(&file_stat.st_ctime));
  25. return 0;
  26. }
  27.  
  28.  
Nov 5 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.