I want to create 2 directories one inside another. Directories will have randomly generated names. But I've encounted some strange behaviour of stat() function.

Each call to stat() for dirname_ returns 0. But there are no files inside workdir_. Still can't find out what is wrong in my code.

Compiled on Linux computer 3.8.0-26-generic #38-Ubuntu SMP Mon Jun 17 21:43:33 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

Here is the source:

#include <string>
#include <stdexcept>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>


const std::string _randomStr(const int len) {
    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    std::string s(len + 1, 0);
    for (int i = 0; i < len; ++i) {
        s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
    }
    return s;
}

int main(void)
{
    std::string workdir_;
    std::string dirname_;
    struct stat sb;
    int err = 0;

    do {
        workdir_ = "./" + _randomStr(10);
    } while (0 == ::stat(workdir_.c_str(), &sb));
    err = ::mkdir(workdir_.c_str(), 0755);
    if (err)
        throw std::runtime_error("failed to initialize directory");
    std::cout << "workdir: " << workdir_ << std::endl;

    do {
        dirname_ = workdir_ + "/" + _randomStr(10);
    } while (0 == ::stat(dirname_.c_str(), &sb));
    err = ::mkdir(dirname_.c_str(), 0755);
    if (err)
        throw std::runtime_error("failed to initialize directory");
    std::cout << "dirname : " << dirname_ << std::endl;     
    return 0;
}
有帮助吗?

解决方案

You have a bogus \0 in your dirname_. When you print the directory name inside the loop and run through strace, the output looks like

write(2, "dirname : ", 10)              = 10
write(2, "./zLOZ2nOXpP\0/7U20o0J90x\0", 25) = 25
write(2, "\n", 1)                       = 1
stat("./zLOZ2nOXpP", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0

So, cerr or cout correctly print the string (using its length), but when converting to a C-String with c_str() the result is only the first directory name, which is then passed to stat().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top