-3

I am trying to get the size of a file in C with the following operations... I am new to C

This is my struct mem at the top of my file:

struct mem {size_t size;
            };

Is this the correct set up with local variables/return statements and such?

struct mem* fileSize(char* filename)
{
    currentPos = lseek(filename, (size_t)0, SEEK_CUR);
    size = lseek(filename, (size_t)0, SEEK_END);
    lseek(filename, currentPos, SEEK_SET);   // seek back to the beginning of file
    return size;
}
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
Surz
  • 984
  • 3
  • 11
  • 36
  • You're missing variable definitions. Or if those are global variables, they should be local ones. You should paste complete, compilable code. Or include compiler errors if problem is getting code to compile. – hyde Apr 26 '14 at 04:17
  • Define currentPos right above it's initialization? – Surz Apr 26 '14 at 04:20
  • possible duplicate of [How do you determine the size of a file in C?](http://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c) – chux - Reinstate Monica Apr 26 '14 at 04:26
  • Bear in mind that using `lseek()` like this won't give you the actual size of the file on disk if the file has [holes](http://en.wikipedia.org/wiki/Sparse_file) in it. – Crowman Apr 26 '14 at 04:27
  • to get the number of bytes without altering the file pointer – Surz Apr 26 '14 at 04:27
  • 1
    `lseek()` also doesn't take a `char *` as its first argument. There's so much wrong with this code snippet that it's hard to know where to begin. The logic is approximately right, as far as it goes, but you try to return some kind of integer when you define the function to return `struct mem *`, use the wrong arguments for `lseek()`, etc. – Crowman Apr 26 '14 at 04:33
  • I return size which is part of my struct mem. Isn't that the pointer to the structure? – Surz Apr 26 '14 at 04:38
  • @JohnSmith: No, `size` is a member of the `struct`, it's not a pointer to it. – Crowman Apr 26 '14 at 04:40

3 Answers3

3

From what can be observed, perhaps you would like to know how to pass the filesize back to the caller in a 'mem' structure (of your own design). This is certainly possible; however the 'fileSize()' function will have to supply memory in which to return this 'mem' struct. Perhaps something like:

struct mem* fileSize(char* filename)
{
   struct mem *m = malloc(sizeof(*m));

Add the line above to allocate memory of suitable size.

... and perhaps a small oversite... lseek() does not take a filename as it's first parameter. Rather it requires a 'file descriptor', which can be obtained (from a filename) by implementing 'open()':

   int fd = open(filename, O_RDONLY);

Now, fd can be passed to 'lseek()', instead of the filename.

   off_t currentPos = lseek(fd, (size_t)0, SEEK_CUR);
   m->size = lseek(fd, (size_t)0, SEEK_END);

'm' is a 'struct mem *' where you can now store the size.

   lseek(fd, currentPos, SEEK_SET);   // seek back to the beginning of file

And don't forget to close the file when finished:

   close(fd);

   return(m);

The allocated and initialized 'm' is returned to the caller (with m->size).

}

The caller (of fileSize) should 'free()' the (struct mem) memory when finished with it to prevent a memory leak.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
0

You can use POSIX function stat(2) to find out the size of a file directly. If you want to only use Standard C Library functions, then you should use fseek(3) and ftell(3).

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
0

lseek() does not work from a filename. It expects an integer file handle. Try stat() instead. Additionally, don't use a pointer return value. Something like this:

size_t fileSize(char* filename)
{
    size_t rv = 0;  // I like to return 0, if badness occurs
    struct stat  file_info;

    if ( (filename != NULL) && (stat(filename,&file_info) == 0) )  //NULL check/stat() call
      rv = (size_t)file_info.st_size;  // Note: this may not fit in a size_t variable

  return rv;
}