Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing string through stat() function

Tags:

c++

I have following code:

#include <stdio.h>       // For printf()
#include <sys/stat.h>   // For struct stat and stat()

struct stat stResult;

if(stat("Filename.txt", &stResult) == 0)
{
      // We have File Attributes
      // stResult.st_size is the size in bytes (I believe)
      // It also contains some other information you can lookup if you feel like it
printf("Filesize: %i", stResult.st_size);
}
else
{
      // We couldn't open the file
printf("Couldn't get file attributes...");
}

Now. How can I pass my own string through stat()? Like this

string myStr = "MyFile.txt";
stat(myStr, &stResult)
like image 220
Chris Avatar asked Dec 08 '25 12:12

Chris


1 Answers

If you are talking about a C++ std::string, you might want to use

string myStr = "MyFile.txt";
stat(myStr.c_str(), &stResult)

so use the c_str() member-function of your string object in order to get a C string representation.

like image 172
Savek Avatar answered Dec 10 '25 01:12

Savek