Thread: Problems opening file

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Problems opening file

    I'm having a problem opening a file and I'm thinking it's because of directories or something. I'm working on an image loader and i have a file test.bmp, but whenever I call the function, it fails because fopen is returning 0 meaning it can't find the file, I know it's able to be opened, because I copied the exact same code into a function in the main cpp file and it opened just fine. I have the image loader in a folder called loaders and also in a folder called loaders in my Dev-C++ project. I'm guessing that's causing a problem? Putting the image in the loaders folder didn't work, putting ../test.bmp as the string didn't either. Any idea why it can't find the file, and how I can fix it? Thanks in advance.
    Using Dev-C++ on Windows

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    fopen is C, not C++, reguardless of that posting the function
    and how you calll the function is prolly the very minimum
    of what we need you to include in order to diagnos your problem.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    24
    if your program is called with command line arguments chances are the 'current directory' isn't the program directory. if so, you can extract the application's path from the first command line arg and then just append the bitmap filename to it.

  4. #4
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Sorry for the late reply, been busy and haven't had time to check this. Here's the function, LIMAGE is a struct that contains width, height, type and a char* to hold image data.
    Code:
    LIMAGE BMPLoader::loadBMP(const char* filename) {
        struct {
            unsigned short int type;
            unsigned int filesize;
            unsigned int reserved;
            unsigned int offset;
        } header;
        struct {
            unsigned int headersize;
            unsigned int width, height;
            unsigned short int planes;
            unsigned short int bitsppx;
            unsigned int compression, imagesize, hres, vhres, numcolors, importantcols;
        } infoheader;
        char* palette = 0;
        char* imaged = 0;
        LIMAGE image = {0, 0, 0, 0};
        if (filename == 0) return image;
        FILE* file = 0;
        file = fopen(filename, "rb");
        if (file == 0) return image;
    }
    That is in loaders\bmploader.cpp. In the main CPP, in my init function I have this:
    Code:
    LIMAGE temp = BMPLoader::loadBMP("test.bmp");
    I do a check and temp contains all zeros. I did a test within in the function earlier, the filename was going through fine, for some reason fopen can't locate the file. Any ideas?
    Last edited by Ganoosh; 08-29-2005 at 12:07 AM.
    Using Dev-C++ on Windows

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Cool

    Is test.bmp in a folder that has a space in it? Sometimes that creates problems with certain versions of Windows.

  6. #6
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    Yes it is actually, but I copied the same code to the main cpp and it worked perfectly fine. For some reason the folders just mess it up or something. But I'm gonna try putting it in a spaceless-named folder and see if it works.
    Nope, didn't work.
    Using Dev-C++ on Windows

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What system are you using . . . oh yes, "Dev-C++ on Windows". Try "./filename", although that usually only causes a problem on Linux.

    Try putting the whole path in (ie, "C:\My Documents\files\filename.ext").

    [edit]
    Or rather "C:\\My Documents\\files\\filename.ext"
    [/edit]
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  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. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM