LINUX.ORG.RU

Есть несколько способов.

1. man 2 stat, поле off_t st_size; /* total size, in bytes */

2. lseek(fd,(off_t)0,SEEK_END);

3. fseek(f,0,SEEK_END); ftell(f);

Die-Hard ★★★★★
()

#include <sys/stat.h> /*Уточни сам */ . . . /*Ф-ия возвращает тип int - уточни сам*/ int forsize(char *filename) { struct stat st;

if(stat(filename, &st) < 0) return -1; else return st.st_size; /*printf("Group - %d, User - %d\nThe size of file %s = %d\n",st.st_gid,st.st_uid,buf, st.st_size);*/ }

didjim
()

Есть ещё старый добрый способ, со времён bc3.1 :))

cat fsize.c

#include <stdio.h>

int main(int argc, char **argv)
{
  int bytes;
  FILE *f;

  if(argc < 2 || !argv[1])
  {
    fprintf(stderr, "Filename is empty!\nUsage: fsize <filename>\n");
    return 1;
  }

  f = fopen(argv[1], "r");

  if(!f)
  {
    fprintf(stderr, "Couldn't open %s\n", argv[1]);
    return 1;
  }

  fseek(f, 0, SEEK_END);

  bytes = ftell(f);

  fclose(f);

  printf("Bytes: %d\n", bytes);

  return 0;
}

CKulT
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.