Search code examples
cfopensubdirectorypelles-c

C language equivalent to ./folder?


I am working on a C project in Pelles C and I am looking to fopen csv files within a subdirectory of my project folder. Right now I have my absolute pathway to the files like so:

fopen("F:my/path/projectfolder/datafolder/data.csv", "r");

and this works no problem. But I am looking to make my code adaptable for when I move my project folder (say out of my F:/ usb drive to my C:/ harddrive). I would like to know if there is a way to do this without having to change the absolute file path each time I move my project folder.

Basically I am wondering how to say "currentdirectory/datafolder" like in linux ("./datafolder"). Something like the following lines, which I have tested but do not work:

fopen("./datafolder/data.csv","r");

or

fopen("../datafolder/data.csv","r");

Thoughts?


Solution

  • That is the correct syntax, except it is based on your current working directory.

    • To read the current working directory, you could use: getcwd()
    • To change the current working directory, you could use: chdir()