Author Topic: C++ How to change the default root directory for reading/creating a file  (Read 6544 times)

Offline Rub

  • Single posting newcomer
  • *
  • Posts: 3
Hi everyone.
I have a problem, It seems that the default directory for the files I want to read or write in is set on my Users directory (on mac).
I would like to be able to write relative path from somewhere inside my project folder. (On Xcode, the defaut settings for the files was the same folder as the .exe)
I've searched on forums and I read that changing the Execution Working Directory can resolve, but actually it doesn't (maybe I'm doing it wrong)

I'm with macOs 10.6.8 and code::blocks 13.12 (the latest I found for mac)

So here's my C++ code :
Code
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream monFlux("testReadFile.txt");
    if(monFlux)
    {
        cout<<"ok!"<<endl;
        monFlux<<"it works!!"<<endl;
    }
    else
    {
        cout<<"error file opening"<<endl;
    }

    return 0;
}


writing only the name of the file, it is created in /Users/myname/

So I tried to changed this  Project>Properties>Build Targets >Execution Working Directory
it was .
I change to the absolute path I wanted as a root : /Users/myname/Documents/Programmation/LearningCpp/testReadFile/bin/Debug

here it propose two options relative or absolute, I tried both, the result is always the same, the file is created in /Users/myname/

If someone can help me I would be really grateful!
Thank you very much,
Rub
« Last Edit: August 18, 2019, 03:30:56 pm by Rub »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Sadly we have not really any mac developer here. This should be the correct setting for the project...

can you try the function
Code
getcwd
in your code and print the current working directory, and look if it changes if you change it in the project settings?

In the nightly forums there should be a download link for current mac nightlies. I do not know if it is a version for your OS. you can try to have a look at this version...

Offline Rub

  • Single posting newcomer
  • *
  • Posts: 3
Thank you for your answer!
Quote
This should be the correct setting for the project...
Do you mean that the absolute path I set in the project properties should be the right way to do that?

But I have the same results with the three different paths I tried to set in the project properties...

Code
char *path = NULL;
   size_t size;
   path = getcwd(path,size);
   cout<<path;

   free(path);

path always results /Users/username



I searched a bit the nightly build but it seems that 13 is the earliest for mac. I'll search more info maybe on mac code blocks users forums (does that even exist??)

If anyone has a clue, please help!

Thanks anyway :)

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Quote
Do you mean that the absolute path I set in the project properties should be the right way to do that?
yes

Quote
I searched a bit the nightly build but it seems that 13 is the earliest for mac. I'll search more info maybe on mac code blocks users forums (does that even exist??)
http://forums.codeblocks.org/index.php/topic,23428.msg159512.html#msg159512
is OS X not mac?