开发者

Can't open text file resource in Xcode 4 c++ tool?

I am working on figuring out how to use Xcode 4 to debug c++ projects.

I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.

However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.

I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)

The problem I am running into is that the files that are supplied as command line arguments are failing to open.

    ifstream in;
    ofstream out;
    in.open(argv[1]);
    out.open(argv[2]);

I have checked to make sure that the arguments are being properly passed and are named correctly. The 开发者_C百科ifstream in is being supplied with `numbers.txt', which I want to open a text file that I already have.

However when I check to make sure the ifstream is open:

    if(in.is_open() == false){
          cerr << "Unable to open input file" << endl;
          return 1;
    }

I get the error.

I suspect this has something to do with how Xcode organizes the project.

my numbers.txt file is just sitting in the Xcode project folder, and I have only one .cpp class and one product, the executable.

anyone know what I am missing here?


The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.


Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase. Here is an screenshot of the final result (note the destination, subpath and checkbox):

Can't open text file resource in Xcode 4 c++ tool?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜