C++:boost/filesystem:一些问题

3

我正在使用Boost库在Ubuntu环境下编写C++程序。我有一些问题不是很清楚:

  1. fs::is_directory

    namespace fs = boost::filesystem;
    
    
    fs::path full_path(fs::initial_path<fs::path>() );
    
    
    full_path = fs::system_complete(fs::path( "temp/"));
    
    
    if(fs::is_directory(full_path ))
    {
       cout << "the path is a directory" << endl;
    }
    else
    {
       cout << "the path is not a directory" << endl;
    }
    

    => I am sure that the moment I am running the program, there is a directory temp at the same location with the executable file. But it is always returned: "the path is not a directory" ?

  2. fs::last_write_time

    • Is this fs::last_write_time(path) be able to get the last date time of modifying for the given path for BOTH either a directory or a file?

    • If it is true also for a directory, is that true for only the directory when it was created or the last date time if I add a file to inside the folder as well?

  3. fs::directory_iterator

    • fs::directory_iterator dir(full_path) => how can I check whether this 'dir' has any sub directories or not?
  4. Is there any way in boost::fileSystem to check if a file is opening?

提前感谢,希望您能帮助我澄清我的想法!


/tmp 是一个目录,但是 temp/ 是一个目录吗? - Benoit
我重新格式化了你的问题,请看看它的源代码现在是什么样子的 :) - P Shved
谢谢Pavels!我也重新更新了我的问题。 - olidev
不要只是感谢他,还可以继续工作,并格式化你编写的新代码。 ;)(对于小的内联代码片段,请用 `反引号` 包围它们,对于代码块,请将其缩进 4 个空格,在两种情况下,选择它并单击 101010 按钮) - jalf
不确定是否有帮助,但这里有一些使用Boost.Filesystem v3递归查找目录中所有文件的代码:https://github.com/dlidstrom/freedb/blob/master/freedb-pp/freedbpp/main.cpp。 - Daniel Lidström
2个回答

2
  1. 看起来应该可以运行。为什么不在if之前加上cout << fullpath,确保路径真的包含你认为的内容?

  2. 我从未使用过last_write_time,无法帮助你。

  3. 你需要遍历目录的内容,并使用fs::is_directory(dir->status())来确定给定的目录条目是一个目录还是其他文件。(假设dir是你的目录迭代器)

  4. 我不认为boost::filesystem中有任何东西可以告诉你文件是否已经打开。


感谢您简明扼要的评论! - olidev
有人能帮忙回答第2和第4个问题吗? - olidev

0

问题4:你不能通过调用[boost::filesystem|std]::fstream::is_open()来判断一个文件是否已经打开吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接