bandera
bandera

Reputation: 49

Time modification linux c++

I am trying to get the date of the last modification of the files of a folder in C++. But I don't understand how I can replace "afile.txt" but a variable name.

When I replace "afile.txt" by something else I have got this error:

proj.cpp: In function ‘Folder getdir2(std::string, std::vector >&, std::string, std::string, std::string)’: proj.cpp:325:25: error: cannot convert ‘const string {aka const std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘int stat(const char*, stat*)’ stat(t1, &attrib); // get the attributes of afile.txt

Here is the code:

struct tm* clock;       // create a time structure
        struct stat attrib;     // create a file attribute structure    
        stat("afile.txt", &attrib);   // get the attributes of afile.txt
        clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure

Upvotes: 0

Views: 87

Answers (1)

nneonneo
nneonneo

Reputation: 179402

It appears that you are attempting to pass a std::string to stat. stat is a C function, and as such only accepts const char * (a "C" string) as input.

Use the .c_str() method of std::string to get a C string:

std::string filename;
...
stat(filename.c_str(), &attrib);

Upvotes: 1

Related Questions