开发者

Passing string through stat() function

I have following code:

#include <stdio.h>       // For prin开发者_开发技巧tf()
#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)


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.


Use the c_str() member function.

myStr.c_str()

http://www.cplusplus.com/reference/string/string/c_str/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜