Thread: Loading files with a function call

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Loading files with a function call

    I don't know what call the subject, but I have another question, a more basic one. I have a function that loads files into memory (BMPs and later TGAs), but need to have some parts as dynamic, based on parameters in a function.

    Code:
    char LoadFile(char FileName[64]) // accepts a string as a parameter
    {
    	char BasePath[128]; // the base path, that of which started from
    	char FullFileName[192]; // the full path combining the base path and the parameter
    	
    	strcpy(BasePath, "C:\\My Documents\\My programs\\"); // get the obvious base path first
    	sprintf(FullFileName, "%s%s", BasePath, FileName); // combine the base path and file name
    	FileHandle = fopen(FullFileName, "rb"); // read the source file to display, binary mode
    	
    	if (FileHandle == 0) // if the file can't be found
    	{
    		return 1; // indicates an error occurred
    	}
    	
    	fseek(FileHandle, 14, SEEK_SET); // skip the basic file header data but read from the info header
    	fread(&BMPHead.biSize, 4, 1, FileHandle); // bytes 0E through 11
    	fread(&BMPHead.biWidth, 4, 1, FileHandle); // 12 through 15
    	fread(&BMPHead.biHeight, 4, 1, FileHandle); // 16 through 19
    	fread(&BMPHead.biPlanes, 2, 1, FileHandle); // 1A and 1B
    	fread(&BMPHead.biBitCount, 2, 1, FileHandle); // 1C and 1D
    	fread(&BMPHead.biCompression, 4, 1, FileHandle); // 1E through 21
    	fread(&BMPHead.biSizeImage, 4, 1, FileHandle); // 22 through 25
    	fread(&BMPHead.biXPelsPerMeter, 4, 1, FileHandle); // 26 through 29
    	fread(&BMPHead.biYPelsPerMeter, 4, 1, FileHandle); // 2A through 2D
    	fread(&BMPHead.biClrUsed, 4, 1, FileHandle); // 2E through 31
    	fread(&BMPHead.biClrImportant, 4, 1, FileHandle); // 32 through 35
    	fread(&TestImageData, 1, BMPHead.biSizeImage, FileHandle); // read the main image data // bytes 36 and onward
    	fclose(FileHandle); // close the file releasing the handle
    	return 0; // function succeeded
    }
    The function works as I tested it, but what my problem is is with the "BMPHead" and "TestImageData" elements. What do I do to have parameters for this function to be able to set these. I call the function like this:

    if (LoadFile("mytestimage.bmp")) {// message box for showing load error}

    If I had these particular pointers, structs, or whatever they are involved, I'd have the function called like this:

    if (LoadFile("mytestimage.bmp", BMPHead, TestImageData) {// message box}

    What changes do I need to make to get it to work like this? I know how to get the message box so I don't need any assistance with that.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Depends on how they're declared globally.
    Code:
    char LoadFile(char FileName[64], struct foo *BMPHead, struct bar TestImageData)
    Something like that maybe.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    BMPHead is indeed a struct, but TestImageData is an array (would make it a linked list, but don't know how). They are defined like this:

    BITMAPINFOHEADER BMPHead;
    char TestImageData[589824];

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well there you go then. Just move those declarations into the function parameter list.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    What use is there in doing that? Loading the same file 60 times a second is not only very bad for games, but senseless in general, especially when there'll be dozens of files. Would you load 720 files per second for just 12 files? I highly doubt it. My hard drive would barely be able to keep up with that for 128x128 32-bit images (45 MiB per second).

    By having a function call where I set the values as needed, it's easier to work with otherwise I'd have to repeat the code dozens of times and it would just get messy, very messy.

    Edit: When calling the function, the file is first loaded into the bitmapinfoheader struct and is filled out. The parameter I use in the function is what struct for this is used and stored so DrawDibDraw can later display the contents. The image data is then loaded into an array (linked list if I knew how). Each image loaded points to a different bitmapinfoheader struct and a different array. I would like it so that I can merely call a function and set these parameters in the function call. This makes it easier (and much cleaner-looking) to load dozens images and keep them in memory.
    Last edited by ulillillia; 04-10-2007 at 03:07 PM. Reason: Clarified what I'm after

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why would you load the same file multiple times? I'm suggesting changing something like:
    Code:
    struct foo bar;
    int some_global_var;
    
    void load_files(char *filename)
    {
      // read stuff into global struct and int
    }
    
    int main(void)
    {
      load_files("blee.bmp");
      return 0;
    }
    ...to something like this:
    Code:
    void load_files(char *filename, struct foo bar, int var)
    {
      // read stuff info local struct and int
    }
    
    int main(void)
    {
      struct foo bar;
      int var;
    
      load_files("blee.bmp", bar, var);
      return 0;
    }
    See the difference?
    If you understand what you're doing, you're not learning anything.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Edit: Okay, so I had the wrong pointer type in the struct. I forgot it's ->. I also had "BMPdata" instead of "BMPData" which, strangely enough, messed things up (must be case sensitive). Now I'm getting an "unhandled exception" error and it asks me if I want to look at the disassembly. I look at it but have no idea what any of that is. What is causing this to happen?

    Code:
    char LoadFile(char FileName[64], BITMAPINFOHEADER *BMPInfo, char *BMPData) // accepts a string as a parameter
    {
    	char BasePath[128]; // the base path, that of which started from
    	char FullFileName[192]; // the full path combining the base path and the parameter
    	
    	strcpy(BasePath, "C:\\My Documents\\My programs\\"); // get the obvious base path first
    	sprintf(FullFileName, "%s%s", BasePath, FileName); // combine the base path and file name
    	FileHandle = fopen(FullFileName, "rb"); // read the source file to display, binary mode
    	
    	if (FileHandle == 0) // if the file can't be found
    	{
    		return 1; // indicates an error occurred
    	}
    	
    	fseek(FileHandle, 14, SEEK_SET); // skip the basic file header data but read from the info header
    	fread(&BMPInfo->biSize, 4, 1, FileHandle); // bytes 0E through 11
    	fread(&BMPInfo->biWidth, 4, 1, FileHandle); // 12 through 15
    	fread(&BMPInfo->biHeight, 4, 1, FileHandle); // 16 through 19
    	fread(&BMPInfo->biPlanes, 2, 1, FileHandle); // 1A and 1B
    	fread(&BMPInfo->biBitCount, 2, 1, FileHandle); // 1C and 1D
    	fread(&BMPInfo->biCompression, 4, 1, FileHandle); // 1E through 21
    	fread(&BMPInfo->biSizeImage, 4, 1, FileHandle); // 22 through 25
    	fread(&BMPInfo->biXPelsPerMeter, 4, 1, FileHandle); // 26 through 29
    	fread(&BMPInfo->biYPelsPerMeter, 4, 1, FileHandle); // 2A through 2D
    	fread(&BMPInfo->biClrUsed, 4, 1, FileHandle); // 2E through 31
    	fread(&BMPInfo->biClrImportant, 4, 1, FileHandle); // 32 through 35
    	fread(BMPData, 1, BMPInfo->biSizeImage, FileHandle); // read the main image data // bytes 36 and onward
    	fclose(FileHandle); // close the file releasing the handle
    	
    	return 0;
    }
    
    LoadFile("ulillilliacity.bmp", &BMPHead, &TestImageData); // function called
    Edit: Never mind, I fixed the problem in full. I had "&BMPData" instead of "BMPData". I seem to need more practice with pointers....
    Last edited by ulillillia; 04-10-2007 at 08:46 PM. Reason: Resolved the issue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM