如何获得它是由命令行参数调用文件所在的目录? [英] How to get directory of a file which is called from command line argument?
问题描述
例如,我打电话给我的可执行文件在Ubuntu:
For example, I am calling my executable in ubuntu:
./富温度/ game1.txt温度/ TXT GAME2
./foo temp/game1.txt temp/game2 txt
我使用的真实路径()来查找路径game1.txt。
使用但是它会给我的完整路径,包括game1.txt名称。
例如,它会出来为
I am using realpath() to find the path to the game1.txt. Using it however will give me the full path including the game1.txt name. For example, it will come out as
/家/的 * 的/Download/temp/game1.txt
/home/*/Download/temp/game1.txt
我要删除该字符串game1.txt,这样我可以使用字符串用它来输出其他文件文件夹中。
两个问题。
I want to erase game1.txt on that string so that I can use the string to use it to output other files in that folder. Two questions.
- 的真实路径是()好吗用于这种操作?有没有更好的办法?
- 能有人给我一个快速的方法来消除game1.txt,使该字符串将是/家/的 * 的/下载/温度/保存在一个字符串格式(不是char) ?
- Is realpath() alright to use for this kind of operation? Is there better way?
- Can someone give me a quick way to erase "game1.txt" so that the string will be "/home/*/Download/temp/" save in a string format(not char)?
非常感谢你。
推荐答案
真的不知道Linux的,但是对于你的第二个问题的一般方法:
Don't really know Linux, but a general way for your second question:
#include <string>
#include <iostream>
int main(){
std::string fullpath("/home/*/Download/temp/game1.txt");
size_t last = fullpath.find_last_of('/');
std::string path = fullpath.substr(0,last+1);
std::cout << path;
}
参见Ideone 。
这篇关于如何获得它是由命令行参数调用文件所在的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!