2011-05-19 172 views
0

我有以下代码:传递字符串

#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..."); 
} 

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

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

回答

7

如果你谈论的是C++的std :: string,你可能想使用

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

所以使用String对象的c_str()成员函数为了得到一个C字符串表示。