Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

C++ How to change the default root directory for reading/creating a file

(1/1)

Rub:
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;
}


--- End code ---

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

BlueHazzard:
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
--- End code ---
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...

Rub:
Thank you for your answer!

--- Quote ---This should be the correct setting for the project...
--- End quote ---
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);

--- End code ---

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 :)

BlueHazzard:

--- Quote ---Do you mean that the absolute path I set in the project properties should be the right way to do that?
--- End quote ---
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??)
--- End quote ---
http://forums.codeblocks.org/index.php/topic,23428.msg159512.html#msg159512
is OS X not mac?

Navigation

[0] Message Index

Go to full version