File creation location on Mac

Discussion in 'Mac Programming' started by BigDogUK, Sep 2, 2005.

  1. BigDogUK macrumors newbie

    Apr 4, 2005
    Hey all. I've been teaching myself a little C++ and for whatever reason I was doing it on my Windows PC mostly. Well, that blew up (not joking :( ) yesterday so I'm moving to my lovely reliable Mac.

    Just one question. In C++ when I want to output a file using this:

    ofstream newFile("records.txt", ios::app);

    How do I make it so the file is created in the same directory and the unix executable? I tired:

    ofstream newFile("./records.txt", ios::app);

    . = current directory in Unix? but that didn't work... it keeps creating them in the root directory for the user instead. On Windows it automatically defaulted to creating the file in the same directory.

    Suggestions welcome! Thanks!
  2. BigDogUK thread starter macrumors newbie

    Apr 4, 2005
    ahh, it works fine if I execute the unix exec from the terminal because then I'm IN the current directory. Just if I double click it it doesn't change directory to the right one.

    Is there any easy way to sort this? Not really a big problem but would be nice to be able to run it by just double clicking.
  3. yippy macrumors 68020


    Mar 14, 2004
    Chicago, IL
    You can either hard code the path into the executable or possibly use a search to locate the executable file and then pipe the output path to the previous command as the save path.

    Not exactly sure of the sintax for that but if you know unix you should be able to figure it out. If not I might be able to try for you but there are probably others who could help more.

    Edit: Realized that the using pwd won't help cause using ./ is the same thing.
  4. WebMongol macrumors member

    Sep 19, 2004
    Bay Area, CA
    By default your new file is created in a 'current directory'. If you want to create the file in the directory that contains a program file you need to find this directory name. The easiest way is to parse the argv[0] that contains pathname to the program and provide a full pathname (directory + filename) to the ofstream constructor.

    int main(int argc, char** argv)
    printf("Program pathname: %s\n", argv[0]);
    return 0;
  5. BigDogUK thread starter macrumors newbie

    Apr 4, 2005
    Thanks! That's definately what I needed. I've almost got it working. At the moment I get an error when I pass a string variable to the ofstream or ifstream. Code goes roughly like this:

    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <iostream>
    #include <vector>
    using namespace std;

    int addrecord(string dir);
    int main(int argc, char** argv)

    string dir = ("%s", argv[0]);


    int addrecord(string dir)


    string keydir = dir + "/keycount.txt";
    string recdir = dir + "/records.txt";
    ifstream myFile(keydir);

    Now, for some reason the ifstream doesn't like keydir in there. Works fine if I replace keydir with "./keycount.txt"/ But otherwise, I get all these errors which I'm having trouble trying to figure out:

    database.cpp: In function `int addrecord(std::string)':
    database.cpp:68: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'
    /usr/include/gcc/darwin/4.0/c++/fstream:525: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/gcc/darwin/4.0/c++/fstream:511: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/gcc/darwin/4.0/c++/iosfwd:90: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)

    It seems to be moaning about CHARS. Does it want a char variable or something instead? Anyone got any suggestions. I'm pretty new to C++ so forgive my lack of skill.

Share This Page