OghmaOsiris OghmaOsiris -4 years ago 90
C++ Question

C++ ifstream on XCode: Where is the default directory?

Ok, so this is the first time I've coded C++ in Xcode (I'm used to ObjC)and I've now started a programming course at my college.

I'm trying to open a file (either hard coded or from user input in the console) and no matter what I try, it says the file won't open (through error checking)

I'm assuming it's because the test.txt file I have isn't in the assumed root directory, so if that's the case, what is the root directory?

Here's my code so far:

//include files
#include <iostream>
#include <stdio.h>
#include <fstream>

using namespace std;

//Global Variables
short inputPicture[512][512];
short outputPicture[512][512];

//Function Prototypes
void getInput(char* in, char* out);
void initializeArray(ifstream* input);

int main(){
//local variables
char inputFile[32];
char outputFile[32];

ifstream input;
ofstream output;

getInput(inputFile, outputFile);
cout << inputFile << endl;//test what was sent back from the function

input.open(inputFile, ifstream::in);
if (!input.is_open()){//check to see if the file exists
cout << "File not found!\n";
return 1;//if not found, end program


return 0;
}//end Main

//Gets initial input from user
void getInput(char* in, char* out){
cout << "Please designate input file: ";
cin >> in;
cout << "\nPlease designate an output file: ";
cin >> out;
}//end getInput

//Sets the global array to the information on the input file
void initializeArray(ifstream* input){

}//end initializeArray

Please let me know if there's something else wrong I'm doing, as I'm sure that's always a great possibility :)

Answer Source

The default directory should be relative the application's working directory, which is usually the same place the application is located (debuggers can mess with that, sometimes).

For simple testing, just specify an absolute path in the command line (or code).

To get the current directory (to see), the getcwd() C function (also usable in C++) will help. Something like:

char * dir = getcwd(NULL, 0); // Platform-dependent, see reference link below    
printf("Current dir: %s", dir);

That should display it in the console. The getcwd function has a few variations depending on what you run on, I've not tested on Mac, but info here:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download