如何将工作目录与用户指定的文件(w或w/o路径)结合起来以获取文件的up-dir

发布于 2024-11-08 12:42:15 字数 804 浏览 3 评论 0原文

目前我正在编写一种库,它从文件名“sFilename”外部获取。使用它,数据被写入将创建的文件,数据被附加到带有数据的现有文件中,数据被更新到带有数据的现有文件中,或者数据被从现有数据中读取。

使用我的库的应用程序的用户应该获得尽可能多的有关文件处理错误的信息。

为此,我编写了一个方法 FileExists(...) ,它使用 _stat() 来确定文件是否存在,以及一个方法“bool checkPermission(std::string sFilename, CFile::EOpenmode iOpenmode)”,它返回一个bool 是否具有 iOpenmode (Read、Write、Readwrite) 的指定文件 (sFilename) 具有读、写或读写权限。

此方法也可与 _stat(sFilename.c_str(), &buf) 配合使用,并在 buf 中返回所需的文件信息。

在检查任何文件之前,我想检查包含指定文件的目录是否具有所需的权限,为此我想对目录使用 checkPermission 方法 [与 _stat() 配合使用]!

现在的问题是:如何轻松确定包含的目录?如果用户仅给出文件名“test.txt”,则文件将在工作目录中创建或读取。所以很容易得到up-directory。它与工作目录相同。 (使用 checkPermission 来获取有关目录的详细信息很简单)。

但是,当用户不仅给出文件名时怎么办?例如“....\test.txt”或“dir1\dir2\test.txt”。如何将工作目录与特定路径结合起来获取文件的上层目录,然后进行权限检查?

唷,我希望一切都清楚了,而且时间不会太长;-)

Rumo

at the moment I'm writing a kind of lib, which gets from outside the file name 'sFilename'. With it data were written to a file it will be created, data were append to an existing file with data, data were updated in an existing file with data or the data were read from an existing data.

The user of the application with my lib should get as much as possible on information about errors of file handling.

For this purpose I code a method FileExists(...) which uses _stat() to determine if a file exists or not and a method "bool checkPermission(std::string sFilename, CFile::EOpenmode iOpenmode)" which gives back a bool if the specified file (sFilename) with the iOpenmode (Read, Write, Readwrite) have the permission to be read, written or read and written.

This method works with _stat(sFilename.c_str(), &buf) too and gives the desired information of the file back in buf.

Before checking any file I want to check if the directory containing the specified file has the desired permissions and for that I want to use the checkPermission method [works with _stat()] for the directory!

Now the problem: how can I determine easyly the containing directory? If the user just give a filename "test.txt" the file will be created or read in working directory. So its easy to get the up-directory. Its the same like the working directory. (And there its simple to use checkPermission to get detailed information about the directory).

But what about when the user not only give the file name? For exaample "....\test.txt" or "dir1\dir2\test.txt". How to combine the working directory with a specific path to gain the up-directory of the file and then to check the permissions?

Phew, I hope all is clear and it was'nt too long ;-)

Rumo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无畏 2024-11-15 12:42:15

我建议使用 www.boost.org 上的 Boost FileSystem 库。特别是,请查看 path 类,它具有 make_absoluteparent_path 等方法。

I'd suggest using the Boost FileSystem library at www.boost.org. In particular, check out the path class, which has methods such as make_absolute and parent_path.

泪是无色的血 2024-11-15 12:42:15

这是 Windows 示例代码 GetFileNameFromHandle,向您展示如何从 HANDLE 获取路径。我想这就是你正在寻找的。

http://msdn.microsoft.com/en -us/library/aa366789%28v=vs.85%29.aspx

This is Windows example code GetFileNameFromHandle to show you how to get the path from a HANDLE. I think it is what you are looking for.

http://msdn.microsoft.com/en-us/library/aa366789%28v=vs.85%29.aspx

宣告ˉ结束 2024-11-15 12:42:15

我发现 _stat() 和 _access() 并不真正适用于目录的权限。请参阅此 stackoverflow 页面

使用 _stat() 时,您不能使用“.\”来获取有关当前目录的信息。但 _access() 至少可以检查目录是否存在“.\”或“..\”。

总之,我使用 _access() 来检查目录是否存在,并使用 _stat() 来检查现有文件的权限。如果应该创建一个文件,我会通过执行来检查它。

顺便说一句;-) 我不需要将工作目录与用户指定的文件结合起来,因为我可以在 _access() 中单独使用指定的文件来确定目录是否存在。

鲁莫

I found out that _stat() and _access() doesn't really works for the permissions of the directories. See this stackoverflow page.

With _stat() you can't use ".\" to get information about the current directory. But _access() at least can check if a directory exists as well ".\" or "..\".

In conclusion I use _access() to check the existence of a directory and _stat() to check the permissions of an existing file. If a file should be created I'll check it by doing.

And by the way ;-) I don't need to combine working directory with the user specified file because I can use the specified file alone in _access() to determine if directory exists.

Rumo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文