需要相关文件路径的帮助 [英] Need help with relative file path

查看:57
本文介绍了需要相关文件路径的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


我正在写一个需要读取文件的c ++程序。我正在尝试阅读与可执行文件位于同一目录中的
a文件。


如果我在程序中执行程序,一切正常's
目录。我需要做的是读取该文件,无论在哪里

(cwd)我执行程序,而不是硬编码绝对的,因为

程序可能被移动或处于其他

电脑上的不同位置。


我该怎么办?


PS。如果它有帮助,我正在使用boost文件系统框架。

解决方案

6月21日,8:11 * am,Thomas B?rlum< bor ... @ gmail.comwrote:


嘿所有,


我正在写一个需要的c ++程序阅读文件。我正在尝试阅读与可执行文件位于同一目录中的
a文件。


如果我在程序中执行程序,一切正常's
目录。我需要做的是读取该文件,无论在哪里

(cwd)我执行程序,而不是硬编码绝对的,因为

程序可能被移动或处于其他

电脑上的不同位置。


我该怎么办?


PS。如果它有帮助我正在使用boost文件系统框架。



大多数操作系统将为您提供用于执行程序的命令

argv [0]

这通常包括路径


int main(int argc,char * argv [])

{

std :: cout<< argv [0]<< \ n \ nn;

}


NB。它可能是绝对的或相对的。


Martin York写道:


大多数操作系统会给你用于执行程序的命令

argv [0]

这通常包括路径



实际上,argv [0]很简单就是你在命令行中用

编写的程序名称,没有任何附加内容。

< br>

2008-06-21 17:50:33 +0200,Martin York< Ma *************** @ gmail.comsaid:
< blockquote class =post_quotes>
6月21日,8:11 * am,Thomas B?rlum< bor ... @ gmail.comwrote:


>嘿所有,

我正在编写一个需要读取文件的c ++程序。我正在尝试阅读与可执行文件位于同一目录中的文件。

如果我在程序中执行程序,一切正常。
目录。我需要做的是读取该文件,无论我在哪里执行程序,而不用硬编码,因为程序可能被移动或处于其他地方的不同位置

电脑。

我该怎么办?

PS。如果它有帮助我正在使用boost文件系统框架。



大多数操作系统将为您提供用于执行程序的命令

argv [0]

这通常包括路径


int main(int argc,char * argv [])

{

std :: cout<< argv [0]<< \ n \ nn;

}


NB。它可能是绝对的或相对的。



感谢让我走上正轨。我已经完成了以下工作:


path program_path(string(argv [0])+" /..& quot;);

program_path = complete(program_path);

string settings_file = program_path.string()+" / setting.txt" ;;


效果很好。

Hey all,

I''m writing a c++ program that needs to read a file. I''m trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program''s
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I''m using the boost filesystem framework if it helps.

解决方案

On Jun 21, 8:11*am, Thomas B?rlum <bor...@gmail.comwrote:

Hey all,

I''m writing a c++ program that needs to read a file. I''m trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program''s
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I''m using the boost filesystem framework if it helps.

Most OS''s will give you the command used to execute your program in
argv[0]
This will usually include the path

int main(int argc,char* argv[])
{
std::cout << argv[0] << "\n\n";
}

NB. It may be absolute or relative.


Martin York wrote:

Most OS''s will give you the command used to execute your program in
argv[0]
This will usually include the path

Actually it''s rather usual that argv[0] is simply what you wrote in
the command line as the program''s name, without any additions.


On 2008-06-21 17:50:33 +0200, Martin York <Ma***************@gmail.comsaid:

On Jun 21, 8:11*am, Thomas B?rlum <bor...@gmail.comwrote:

>Hey all,

I''m writing a c++ program that needs to read a file. I''m trying to read
a file that is in the same directory as the executable.

Everything works fine if I execute the program while in the program''s
directory. What I need to do is read that file regardless of where
(cwd) I execute the program from, without hardcoding the absolute since
the program might be moved or be in differant locations on other
computers.

What should I do?

PS. I''m using the boost filesystem framework if it helps.


Most OS''s will give you the command used to execute your program in
argv[0]
This will usually include the path

int main(int argc,char* argv[])
{
std::cout << argv[0] << "\n\n";
}

NB. It may be absolute or relative.

Thanks that got me on the right track. I''ve done the following:

path program_path(string(argv[0]) + "/..");
program_path = complete(program_path);
string settings_file = program_path.string() + "/settings.txt";

works great.


这篇关于需要相关文件路径的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆