Wiki
Clone wikiiit-cs525-assignment / Storage Manager (from Assignment 1)
Storage Manager storage_mgr.c
Ways to open files, and get its size
Method 1: Standard C Library
see here
#include <stdio.h>
Use fopen()
to open the file.
FILE *fp; fp = fopen(fileName, "rb+"); if (!fp) { return FAILED; }
Use fseeko()
and ftello()
to get filesize.
fseeko(fp, 0, SEEK_END); off_t size = ftello(fp); fseeko(fp, 0, SEEK_SET);
Method 2: POSIX File Descriptors
see here
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h>
Use open()
to open the file.
int fd; fd = open(fileName, O_RDWR); if (-1 == fd) { return FAILED; }
Get filesize.
struct stat buf; fstat(fd, &buf); int size = buf.st_size;
If a FILE
pointer is needed, use fdopen()
to open that file.
FILE *fp; fp = fdopen(fd, "rb+");
Updated