C ++ fopen relative Path

I read some topic about the relative path, but I'm still wrong. Hope sb can help me :). I am using Visual studio 2013, windows 7

I got the following directories:

Here is my .exe file D: \ uni \ C ++ \ ex5 \ msvc2013 \ ex5 \ Debug

Here is the file I want to read D: \ uni \ C ++ \ ex5 \ Res \ thehead.raw

Code to open the file:

FILE* f;
f = fopen("..\\..\\..\\res\\thehead.raw", "rb");
if (f == NULL)
printf("FAIL!!");

Since I need to use relative paths, I figured this out as follows: .. \ gets to the parent directory.

so that ".. \ .. \ .. \" should get into the folder "D: \ uni \ C ++ \ ex5 \".

\ res should open res foulder.

Needless to say, this fails, and I have no idea why. Any help would be appreciated.

+4
source share
2 answers

@Käptn , - , , C, D, D. , , " ", . , , , Debug.

Here is my .exe file C:\uni\c++\ex5\msvc2013\ex5\Debug    
Here is the file i want to read C:\uni\c++\ex5\res\thehead.raw

#include <iostream>

int main (int argc, char ** argv)
{
    FILE* f;
    fopen_s(&f, "..\\..\\..\\res\\thehead.raw", "rb");
    if (f == NULL)
    {
        printf("FAIL!!");
    }
    else
    {
        printf("File opened.");
    }
    return 0;
}
+1

, . - , .

, argv[0], chdir() .

+3

Source: https://habr.com/ru/post/1547317/


All Articles