Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VitaliBR

macrumors newbie
Original poster
Feb 24, 2012
2
0
I created a program using the "Command Line Tool" in Xcode4, with C++

Code:
#include <iostream>
#include <fstream>

int main (int argc, const char * argv[])
{
    //Creates an instance of ofstream, and opens example.txt
    std::ofstream a_file ( "example.txt" );
    // Outputs to example.txt through a_file
    a_file<<"This text will now be inside of example.txt";
    // Close the file stream explicitly
    a_file.close();
    
    return 0;
}

When I run it by xcode, it works perfectly and generates the txt file in:
/Users/<user>/Library/Developer/Xcode/DerivedData/<projectname>-crivblennrpgzoarxmkowljdzvmv/Build/Products/Debug

But if I try to run the program directly from the folder, the file is not generated txt

Why?

I created the release version too, but does not run properly (does not generate the txt file)
and in the Release folder is created one file <nameproject>.dSYM

what is unix executable file?
 
Last edited:

VitaliBR

macrumors newbie
Original poster
Feb 24, 2012
2
0
I try:
Code:
    char * dir1 = getcwd(NULL, 0);
    strcat(dir1,"/text1.txt");
    printf("%s \n",dir1);
    myfile1.open(dir1, std::ios::binary);

    char * dir2 = getcwd(NULL, 0);
    strcat(dir2,"/text2.txt");
    printf("%s \n",dir2);
    myfile2.open(dir2, std::ios::binary);

in the executable Release show in terminal:
/Users/vitalimateus/text1.txt
/Users/vitalimateus/text2.txt

How do I open the file that it is in the same folder as the executable?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
I try:
Code:
    char * dir1 = getcwd(NULL, 0);
    strcat(dir1,"/text1.txt");
    printf("%s \n",dir1);
    myfile1.open(dir1, std::ios::binary);

    char * dir2 = getcwd(NULL, 0);
    strcat(dir2,"/text2.txt");
    printf("%s \n",dir2);
    myfile2.open(dir2, std::ios::binary);

in the executable Release show in terminal:
/Users/vitalimateus/text1.txt
/Users/vitalimateus/text2.txt

How do I open the file that it is in the same folder as the executable?
If you double-click the executable from the Finder, it should behave as you're expecting it to. If you're running it from the Terminal, it inherits the Terminal's current working directory, which defaults to your home folder.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.