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

BigDogUK

macrumors newbie
Original poster
Apr 4, 2005
5
0
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!
 

BigDogUK

macrumors newbie
Original poster
Apr 4, 2005
5
0
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.
 

yippy

macrumors 68020
Mar 14, 2004
2,087
3
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.
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
BigDogUK said:
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!

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

BigDogUK

macrumors newbie
Original poster
Apr 4, 2005
5
0
WebMongol said:
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;
}

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]);
addrecords(dir);

}

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.