通过stat()函数传递字符串

Chr*_*ris 0 c++

我有以下代码:

#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...");
}
Run Code Online (Sandbox Code Playgroud)

现在.如何通过stat()传递我自己的字符串?像这样

string myStr = "MyFile.txt";
stat(myStr, &stResult)
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您正在谈论C++ std :: string,您可能想要使用

string myStr = "MyFile.txt";
stat(myStr.c_str(), &stResult)
Run Code Online (Sandbox Code Playgroud)

所以使用字符串对象的c_str()成员函数来获得C字符串表示.