Question

I'm trying to do something like that in my API:

char* fileName = "./path/to/file.bmp";
Texture* tex = (Texture *) malloc(sizeof(Texture));
FILE* file = fopen(fileName, "rb");

But for some reason file is coming up null.

Is there a way to get this file descriptor with a relative path?

I see many questions about that, but nothing that can give me a way to perform this on OS X.

Was it helpful?

Solution 2

I found a solution. My project folder is something like this:

project
|_ src
|_ examples
   |_ example1
      |_ main.c
      |_ assets
         |_ file.bmp

I was trying to get the file descriptor from the main.c. The filePath would be assets/file.bmp, but this isn´t true; I can get the file descriptor with the path from project folder, and the relative file path is examples/example1/assets/file.bmp.

OTHER TIPS

Please check the return value of fopen. i.e

FILE* file = fopen(fileName, "rb");
if(file==NULL)
{
    printf("File can't opened!!");
    exit(0)
}

What do you mean by "can't get the file info" ? All your snippet shows is that you've assigned a filename (which happens to be relative to where your process is running), you've assigned a (Texture *) variable, and acquired a file handle by calling fopen().

All fopen() does is open the file for you. If you want get data from the file, you have to fread() or fscanf() it, eg (assuming that the file contents is binary and that we don't care about whether we read the whole file correctly):

(void)fread(tex, sizeof(Texture), 1, file);

Two other things: #1 I'd call my filehandle something different, like fd or inputfd - it helps to reduce the potential for confusion, and #2 use calloc() rather than malloc() - it zeros out the space allocated for you.

What else does your code do?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top