如何让C_string()工作。 [英] How do I get C_string() to work.
本文介绍了如何让C_string()工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用应该有效的files.c_str()。当我尝试这样做时出现这个错误。
| 50 |错误:'class std :: vector< std :: __ cxx11 :: basic_string< char> >'没有名为'c_str'的成员|
我的尝试:
#include< iostream>
#include< ctime>
#include< sys / types.h>
#include< sys / stat.h>
#include< cerrno>
#include< cstring>
#include< sys / types.h>
#include< dirent.h>
#include< errno.h>
#include< vector>
#include< string>
#include< fstream>
using namespace std;
int getdir(string dir,vector< string>& files)
{
DIR * dp;
struct dirent * dirp;
if((dp = opendir(dir.c_str()))== NULL){
cout<< 错误(<<< errno<<)打开<< dir<< ENDL;
返回错误;
}
while((dirp = readdir(dp))!= NULL){
files.push_back(string(dirp-> d_name));
}
closedir(dp);
返回0;
}
int main(int argc,char ** argv)
{std :: ofstream outputFile;
outputFile.open(Files.txt);
std :: string dir = string(。);
vector< string> files = vector< string>();
getdir(dir,files);
for(unsigned int i = 0; i< files.size(); i ++){
cout<< files [i]<< ENDL;
}
struct stat fileInfo;
if(stat(files.c_str,& fileInfo)!= 0){//此处出现问题<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<
std :: cerr<< 错误:<< strerror(errno)<< \\\
;
}
std :: cout<< 类型::;
if((fileInfo.st_mode& S_IFMT)== S_IFDIR){//来自sys / types.h
std :: cout<< Directory\\\
;
} else {
std :: cout<< File\\\
;
}
std :: cout<< 大小:<<
fileInfo.st_size<< \\\
; //大小(字节)
}
解决方案
if (stat(files.c_str,& fileInfo)!= 0 ){ // 问题
1.文件是一个矢量对象,而不是一个字符串。
2.即使它是一个字符串,c_str也是一个方法,而不是一个varialble字段,因此需要终止括号,即c_str()
I Use files.c_str() which should work. I get this error when I try to do it.
|50|error: 'class std::vector<std::__cxx11::basic_string<char> >' has no member named 'c_str'|
What I have tried:
#include <iostream> #include <ctime> #include <sys/types.h> #include <sys/stat.h> #include <cerrno> #include <cstring> #include <sys/types.h> #include <dirent.h> #include <errno.h> #include <vector> #include <string> #include <fstream> using namespace std; int getdir (string dir, vector<string> &files) { DIR *dp; struct dirent *dirp; if((dp = opendir(dir.c_str())) == NULL) { cout << "Error(" << errno << ") opening " << dir << endl; return errno; } while ((dirp = readdir(dp)) != NULL) { files.push_back(string(dirp->d_name)); } closedir(dp); return 0; } int main(int argc, char** argv ) {std::ofstream outputFile; outputFile.open("Files.txt"); std::string dir = string("."); vector<string> files = vector<string>(); getdir(dir,files); for (unsigned int i = 0;i < files.size();i++) { cout << files[i] << endl; } struct stat fileInfo; if (stat(files.c_str, &fileInfo) != 0) {//Problem here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< std::cerr << "Error: " << strerror(errno) << '\n'; } std::cout << "Type: : "; if ((fileInfo.st_mode & S_IFMT) == S_IFDIR) { // From sys/types.h std::cout << "Directory\n"; } else { std::cout << "File\n"; } std::cout << "Size : " << fileInfo.st_size << '\n'; // Size in bytes }
解决方案
if (stat(files.c_str, &fileInfo) != 0) {//Problem
1. files is a vector object, not a string.
2. Even if it were a string, c_str is a method, not a varialble field, so requires terminating parentheses, i.e.c_str()
.
这篇关于如何让C_string()工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文