1

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?

Madi_FTW
  • 13
  • 2

1 Answers1

3

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()
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • That makes a lot of sense. However I am running into an issue with Pelles C and that library (unistd.h). I get the following error. I tried Google-ing it a bit but I can't seem to find what to do here. ` requires -Go compiler option!` – Madi_FTW Jun 05 '14 at 18:19
  • @Madi_FTW: I won't be able to help you with that. I've never used Pelles C before. You could post a separate question asking how to use `getcwd()` and `chdir()` with your particular compiler. – Bill Lynch Jun 05 '14 at 18:31