How can I get my Cocoa command line tool to know the working path that it was called from in the terminal?

StackOverflow https://stackoverflow.com/questions/15308284

Domanda

The goal of my cocoa command line program is to take an argument which is the name of a file and import it into the program for parsing. I want to be able to call my program lets say, "ParseFile" from the terminal in mac os, with the argument "Filename.txt"(example$ ParseFile filename.txt), but so far I cant understand how my program can know the absolute path to Filename.txt.

For instance when you use 'cp filename.txt /whateverfolder/filename2.txt' you are copying the file 'filename.txt' from the current working directory of the terminal to a folder called whateverfolder on the root directory. How does cp know the absolute path of filename.txt? if i had filename.txt on my desktop cp would know that i sent it /Users/username/Desktop/filename.txt. This is what I want my program to know.

I have been able to get my program to know where it is executed in, but this is very different from where the current working directory of the bash terminal was at the time of being called.

How do i solve this? Thanks a lot

È stato utile?

Soluzione

Use NSFileManager's currentDirectoryPath method:

NSString *currentpath = [[NSFileManager defaultManager] currentDirectoryPath];

Altri suggerimenti

How does cp know the absolute path of filename.txt?

It does not know it and doesn't have to. It can just use the relative path. The current working directory is inherited from the parent process.

I have been able to get my program to know where it is executed in, but this is very different from where the current working directory of the bash terminal was at the time of being called.

What do you mean? The directory where the executable is in? That's usually not of much interest. The only other directory that plays a role in this context is the current working directory. If you already got that, what else do you need?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top