开发者

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

开发者 https://www.devze.com 2023-04-01 05:45 出处:网络
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.

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


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


In the Xcode (v7.1.1 at the time of writing) sidebar, there's an automatically generated folder called "Products". Inside you'll find the executable of your project (assuming you've built your project at least once). Right-click it & choose "Show in Finder". A folder will open in Finder. That's the working directory of your program, & you'll notice it's not actually inside your project's folder.

You can have Xcode use a different directory instead. In the top toolbar, on the left side where it shows your project name next to the active build architecture, click on the Project name > Edit Scheme…

Then look for an option called "Working Directory" in the sheet that appears. Tick the checkbox & then choose a custom directory. Note: Make sure the "Run" option is the selected one in that sheet's sidebar.


The "root" directory that your executable is looking for the file in is not the actual / root directory of the file-system, but is the directory that the executable is executing in ... if you are using Xcode, this may be buried inside one of the build directories automatically created by Xcode for your project rather than a user home folder or home folder sub-directory like /Users/XXXXXX/Documents.


The "default directory" is the directory from which the executable was executed. Usually this is in the same folder as the executable, although if you do stuff like dragging and dropping files on an exe, it can change the startup path.

The path can also change if you're running the program from inside your IDE. The IDE starts the executable, so there's no telling where it's doing it from. You'll have to find where it stores executables and put the file in there, or use an absolute path.


In my case, - getcwd(NULL, 0) returned "/". - and couldn't use abusolute path.(It changes on each terminal deployed.)

So I got the path by ObjC code and throw it to c++ function.

1.Put files in top directory of xcode project. And check they are included in "Targets"->"Build Phases"->"Copy Bundle Resources".

2.Get the path by ObjC code.

NSBundle* bundle = [NSBundle mainBundle];
NSString* resourceDirectoryPath = [bundle bundlePath];
NSString* path = [resourceDirectoryPath stringByAppendingString: @"/"];

3.And throw it to c++ function.

[self cppFunc:[path UTF8String]];

4.Then you can make an ablsolute file path in c++.

std::string file = path(arg) + "filename";

It worked for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消