Thread: Optimisation of reading from file

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    1

    Optimisation of reading from file

    Hi,

    I have created a routine within a C program to load some data from a binary file. The method I am using at the moment is unreliable and slow, and was wondering if there was any way of optimising it?

    I simply open for the file for reading in binary mode and allocate a pointer to an array of unsigned chars - the array size is dependant upon the size of the file. The array is declared as the following...

    Code:
    unsigned char *myArray = (unsigned char*)malloc(sizeof(unsigned char)*size);
    I then read from the file into the array of unsigned chars using the following code...

    Code:
    int c = 0;
    while (c<=size)
    {
        fread(&myQta[c], 1, 1, fp);
        c++;
    }
    The code above simply reads a value one at a time from the file into the array. The method used above works fine and the file is fully loaded into the array. Is there anyway of optimising the load while loop above into something more efficient?

    Regards,
    Bc2005

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You don't need a loop.
    Code:
    fread(myQta, 1, size, fp);
    Kurt

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    unsigned char *myArray = (unsigned char*)malloc(sizeof(unsigned char)*size);
    don't type cast the malloc see FAQ

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM