简体   繁体   English

如何在C ++程序中从文件名命令行参数确定完整路径?

[英]How do you determine full paths from filename command line arguments in a c++ program?

I am writing a program in c++ that accepts a filename as an argument on the command line: 我正在用C ++编写一个程序,该程序在命令行上接受文件名作为参数:

>> ./myprogram ../path/to/file.txt

I know I can simply open an fstream using argv[1] , but the program needs more information about the exact location (ie. full pathname) of the file. 我知道我可以使用argv[1]打开一个fstream,但是程序需要有关文件确切位置(即完整路径名)的更多信息。

I thought about appending argv[1] to getcwd() , however obviously in the example above you'd end up with /path/../path/to/file.txt . 我曾考虑过将argv[1]附加到getcwd() ,但是显然在上面的示例中,您最终会得到/path/../path/to/file.txt Not sure whether fstream would resolve that path automatically, but even if it did, I still don't have the full path without a lot of string processing. 不知道fstream是否会自动解析该路径,但是即使这样做,如果没有大量字符串处理,我仍然没有完整的路径。

Of course, that method wouldn't work at all if the path provided was already absolute. 当然,如果提供的路径已经是绝对路径,则该方法将根本无法工作。 And since this program may be run on Linux/Windows/etc, simply detecting a starting '/' character won't work to determine whether the argument was a full path or not. 而且由于该程序可以在Linux / Windows / etc上运行,因此仅检测开始的'/'字符将无法确定该参数是否为完整路径。

I would think this is a fairly common issue to deal with path names across multiple OSs. 我认为这是处理多个操作系统上的路径名相当普遍的问题。 So how does one retreive the full path name of a command line argument and how is this handled between operating systems ? 那么, 如何获取命令行参数的完整路径名以及如何在操作系统之间进行处理呢?

Pathname handling is highly OS-specific: some OS have a hierarchy with just one root (eg / on Unix ), some have several roots a la MS-DOS' drive letters; 路径名处理是高度特定于OS的:有些OS的层次结构只有一个根(例如/在Unix上),有些OS则具有多个根(例如MS-DOS的驱动器号)。 some may have symbolic links, hard links or other kinds of links, which can make traversal tricky. 有些可能具有符号链接,硬链接或其他类型的链接,这可能会使遍历变得棘手。 Some may not even have the concept of a "canonical" path to a file (eg if a file has hard links, it has multiple names, none of which is more "canonical"). 有些甚至可能没有指向文件的“规范”路径的概念(例如,如果文件具有硬链接,则它具有多个名称,没有一个更像“规范”路径)。

If you've ever tried to do path-name manipulation across multiple OS in Java, you know what I mean :-). 如果您曾经尝试过在Java中跨多个操作系统进行路径名操作,那么您就会明白我的意思了:-)。

In short, pathname handling is system-specific, so you'll have to do it separately for each OS (family), or use a suitable library. 简而言之,路径名处理是特定于系统的,因此您必须针对每个操作系统(系列)分别进行处理,或使用合适的库。

Edit : 编辑

You could look at Apache Portable Runtime , or at Boost (C++ though), both have pathname handling functions. 您可以查看Apache Portable Runtime或Boost(尽管是C ++),它们都具有路径名处理功能。

...you'd end up with /path/../path/to/file.txt. ...您最终会得到/path/../path/to/file.txt。 Not sure whether fstream would resolve that path automatically, but even if it did, I still don't have the full path without a lot of string processing. 不知道fstream是否会自动解析该路径,但是即使这样做,如果没有大量字符串处理,我仍然没有完整的路径。

It does, and you can use /path/../path/ for everything you want without problems. 确实如此,您可以将/path/../path/用于所有您想要的东西而不会出现问题。

Anyway there is no standard function in C++ to do what you want. 无论如何,C ++中没有标准函数可以执行您想要的操作。 You would have to do it manually, and it wouldn't be trivial.. I suggest you keep the path as it is, it shouldn't cause any problems. 您将必须手动执行此操作,而这并非易事。.我建议您保持原样,这不会造成任何问题。

It is OS-dependent. 它取决于操作系统。 If you are using linux you can look at realpath() . 如果您使用的是Linux,则可以查看realpath() No doubt Windows has something comparable. 毫无疑问,Windows具有可比性。

AFAIK there is no standard way. AFAIK没有标准方法。

however you could try this approach (written in pseudocode): 但是,您可以尝试这种方法(用伪代码编写):

string raw_dirname=get_directory_part(argv[1])
string basename=get_filename_part(argv[1])
string cwd=getcwd()
chdir(relative_dirname)
string absolute_dirname=getcwd()
chdir(cwd)
string absolute_filename=absulute_dirname + separator + basename

but note: I am not quite sure if there are issues when symbolic links come into play. 但是请注意:我不确定当符号链接起作用时是否存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM