memory - Getting a file's size in C with lseek? -
I am trying to get the file size in C with the following functions ... I'm new to C
This is my structure meme at the top of my file:
struct mem {size_t size; }; Is this correct set up with local variable / return statements and so on?
struct mem * fileSize (char * filename) {currentPos = lseek file name, (size_t) 0, SEEK_CUR); Size = lseek (filename, (size_t) 0, SEEK_END); Lymph (filename, current page, SEEK_SET); // Back to the beginning of file return size; }
What can be seen, maybe you want to know how to collar a File back into the 'mem' structure (in your own design), it is definitely possible; However, the 'fileSize ()' function must provide memory in which the 'mem' structure must be returned back to something like this:
struct mem * fileSize (char * filename) {struct mem * m = Malloc (sizeof (* m)); Add the above line to allocate the size of the appropriate size.
... and possibly a small overseas ... lseek () file does not take the name because it requires the first parameter instead of 'file descriptor', by implementing 'open' (By file name) can be obtained:
int fd = open (file name, O_RDONLY); Now, in place of the FD file name, 'lseek ()' can be passed.
off_t currentPos = lseek (fd, (size_t) 0, SEEK_CUR); M-> Size = lseek (fd, (size_t) 0, SEEK_END); 'm' is a 'structure meme' where you can now store the size.
lseek (fd, currentPos, SEEK_SET); // Back to the beginning of the file and do not forget to close the file when finished:
close (fd); Return (m); is allocated and returned to the initial 'M' caller (with the size M->).
Memory should be done when it is completed to prevent memory leakage.
Comments
Post a Comment