2011-05-19 173 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字符串表示。