0

Hello here is my problem

FILE *sourcefile;

if ((sourcefile = fopen(argv[1],"r")) == NULL)  //Opens File
{
    printf("Error: Could not open %s\n",argv[1]);
    return 0;
}

fseek(sourcefile, 0 , SEEK_END);  // Sets file pointer at the end of the file
unsigned int fSize = ftell(sourcefile); // Determines file size by the position of file pointer
fseek(sourcefile, 0 , SEEK_SET); // Sets file pointer at the start of the file

char *buffer = (char *) malloc(fSize); 

if (buffer == NULL)
{
    printf("Error: Not enough system memory\n");
    return 0;
}

printf("%d\n",sizeof(buffer));
printf("%d\n",fSile);

fread (buffer,1,fSize,sourcefile);
fclose (sourcefile);

My code is simply opening a file and loading its contents into memory. The problem is, when I use

char *buffer = (char *) malloc(fSize)

it allocates only 4 bytes and not the full size of the file (i.e. 25 bytes when opening a simple txt with a small sentence). When I print the sizes of buffer and fSize at the end, I get 4 and 25 respectively, so fSize is correct. Any idea why this is happening?

Thanks,

Daniel
  • 1,920
  • 4
  • 17
  • 35
apboutos
  • 109
  • 2
  • 16
  • 1
    [Do not cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Chnossos May 05 '14 at 00:56
  • 1
    `"%d"` requires an `int` argument. Use `"%zu"` for `size_t` (such as the result of `sizeof`) and `%u"` for `unsigned int`. `fSize` should be of type `long`, since that's what `ftell` returns. You're assuming that `ftell` will return the size of the file; that's true on some systems, but it's not portable. There is no portable way in C to determine the size of a file. As long as you're writing non-portable code, you might as well use `stat()` or your system's equivalent. – Keith Thompson May 05 '14 at 01:16
  • and open in binary mode, otherwise it's even less reliable than it was – M.M May 05 '14 at 01:39

3 Answers3

4

sizeof(buffer) should be 4 bytes on a 32-bit platform. It is a pointer pointing to the buffer that malloc allocated. There is no way to query it for the size of the buffer.

Dithermaster
  • 6,223
  • 1
  • 12
  • 20
0

sizeof(buffer) is the size of a pointer or 32 bits (4 bytes). It is in actuality allocating enough space.

ojblass
  • 21,146
  • 22
  • 83
  • 132
0

This is maybe what you want to achieve.

#include <stdio.h>
#include <stdlib.h>
int  main(int argc, char *argv[])
{
    FILE *sourcefile;
    if ((sourcefile = fopen(argv[1],"r")) == NULL)  //Opens File
    {
        printf("Error: Could not open %s\n",argv[1]);
        eturn 0;
    }
    fseek(sourcefile, 0 , SEEK_END);  // Sets file pointer at the end of the file
    unsigned long fSize = ftell(sourcefile); 
    fseek(sourcefile, 0 , SEEK_SET); // Sets file pointer at the start of the file
    fSize -= ftell(sourcefile);  /*This determines file size  */
    char *buffer = (char *) malloc(fSize); 
    if (buffer == NULL)
    {
    printf("Error: Not enough system memory\n");
    return 0;
    }

    printf("%ld\n",sizeof(buffer)); 
    printf("%ld\n",fSize);
    fread (buffer,1,fSize,sourcefile);
    fclose (sourcefile);
    return 1;
}