Thread: reading files into memory

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005

    reading files into memory

    Basically, i have a file of an unkown size, with no header etc. to get information from about the size of the file! ...All i need to do is to allocate memory using malloc, creating a pointer to the allocated memory and then read the file char by char into the memory!

    Does anyone know the best way to find the length of the file????

    I have tried a few different ways, but they don't seem to work! This is what i am doing at the moment, but when i print the memory, it is just blank, no data gets put into it!!!!

    FILE *fp;
    char    *buffer;
    long    numbytes;
    /* open an existing file for reading NOTE: filename is not the file i am opening, its just for example!*/
    fp = fopen("filename", "r");
    /* quit if the file does not exist */
    if(fp == NULL)
        return 1;
    /* Get the number of bytes */
    fseek(fp, 0L, SEEK_END);
    numbytes = ftell(fp);
    /* reset the file position indicator to 
    the beginning of the file */
    fseek(fp, 0L, SEEK_SET);	
    /* grab sufficient memory for the 
    buffer to hold the text */
    buffer = (char*)calloc(numbytes, sizeof(char));	
    /* memory error */
    if(buffer == NULL)
        return 1;
    /* copy all the text into the buffer */
    fread(buffer, sizeof(char), numbytes, fp);
    /* confirm we have read the file by
    outputing it to the console */
    printf("contents of file:\n%s", buffer);
    /* free the memory we used for the buffer */

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    University of Waterloo
    fp = fopen("filename", "r");

    Use "rb" instead of "r".

  3. #3
    Registered User
    Join Date
    Aug 2005
    If you want to output the files contents as a string using
    printf("contents of file:\n%s", buffer);
    you will have to append a terminating '\0'.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Use "rb" instead of "r".
    With an extra 'b' the file opens in binary mode (which is required for fread).

    And you shouldn't cast malloc(), calloc(), or realloc().

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM