Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

ifstream

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

//Main
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
    }

    initializeArray(&input);

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

like image 375
OghmaOsiris Avatar asked Sep 02 '11 04:09

OghmaOsiris


People also ask

What is the default directory for Xcode derived data?

~/Library/Developer/Xcode/DerivedData is now the default. You can set the prefs in Xcode to allow projects to specify their build directories.

What is ifstream in C++?

Ifstream is an input stream for files and with it, we can read any information available in the file. For using these stream classes we need to add <iostream> and <fstream> header files in your code. Now let us have a look at the syntax of ifstream classes:

Is there a way to open a directory with ifstream?

And along these same lines, is there a way to open a directory with say ifstream, move through it with a loop, assigning the names of each file in the directory to say an element in a char array, and then display the contents of the array (i.e. the filenames of all the files in the said directory)? Yes, but not with the standard library.

How do I find the build directory in Xcode?

When you select one item, it will display the build output directory absolute path beneath the Derived Data drop-down list. You can click the Advanced… button after the Derived Data directory path to open a Build Location popup dialog to specify the Xcode build directory name and location in detail.


1 Answers

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:

http://linux.die.net/man/3/getcwd

like image 190
ssube Avatar answered Nov 04 '22 21:11

ssube