使用stat列出文件及其信息 我用C++编程了一个FTP服务器,我需要能够获得所有文件的信息: sent: drwxr-xr-x 1000 ubuntu ubuntu 4096 May 16 11:44 Package-Debug.bash
这样我就可以把它寄给客户了。我成功地做到了一部分,但遇到了一些问题。以下是我代码的一部分:使用stat列出文件及其信息 我用C++编程了一个FTP服务器,我需要能够获得所有文件的信息: sent: drwxr-xr-x 1000 ubuntu ubuntu 4096 May 16 11:44 Package-Debug.bash,c++,file,directory,stat,ftp-server,C++,File,Directory,Stat,Ftp Server,这样我就可以把它寄给客户了。我成功地做到了一部分,但遇到了一些问题。以下是我代码的一部分: void Communication::LISTCommand() { DIR *directory; struct dirent *ent; char path[100]; strcpy(path, this->path.c_str()); //this->path can be different from current working path /*if (chdir(path)
void Communication::LISTCommand() {
DIR *directory;
struct dirent *ent;
char path[100];
strcpy(path, this->path.c_str()); //this->path can be different from current working path
/*if (chdir(path) == -1) {
perror("Error while changing the working directory ");
close(clie_sock);
exit(1);
}*/
directory = opendir(path);
struct tm* clock;
struct stat attrib;
struct passwd *pw;
struct group *gr;
string line;
char file_info[1000];
.....
while ((ent = readdir(directory)) != NULL) {
line.clear();
stat(ent->d_name, &attrib);
clock = gmtime(&(attrib.st_mtime));
pw = getpwuid(attrib.st_uid);
gr = getgrgid(attrib.st_gid);
if (S_ISDIR(attrib.st_mode))
line.append(1, 'd');
else line.append(1, '-');
if (attrib.st_mode & S_IRUSR)
line.append(1, 'r');
else line.append(1, '-');
if (attrib.st_mode & S_IWUSR)
line.append(1, 'w');
else line.append(1, '-');
if (attrib.st_mode & S_IXUSR)
line.append(1, 'x');
else line.append(1, '-');
if (attrib.st_mode & S_IRGRP)
line.append(1, 'r');
else line.append(1, '-');
if (attrib.st_mode & S_IWGRP)
line.append(1, 'w');
else line.append(1, '-');
if (attrib.st_mode & S_IXGRP)
line.append(1, 'x');
else line.append(1, '-');
if (attrib.st_mode & S_IROTH)
line.append(1, 'r');
else line.append(1, '-');
if (attrib.st_mode & S_IWOTH)
line.append(1, 'w');
else line.append(1, '-');
if (attrib.st_mode & S_IXOTH)
line.append("x ");
else line.append("- ");
sprintf(file_info, "%s%d %s %s %d %s %d %02d:%02d %s\r\n", line.c_str(), pw->pw_uid,
pw->pw_name, gr->gr_name, (int) attrib.st_size, getMonth(clock->tm_mon).c_str(),
clock->tm_mday, clock->tm_hour, clock->tm_min, ent->d_name);
if (send(c_data_sock, file_info, strlen(file_info), 0) == -1) {
perror("Error while writing ");
close(clie_sock);
exit(1);
}
cout << "sent: " << file_info << endl;
}
.....
}
void通信::LISTCommand(){
DIR*目录;
结构导向;
字符路径[100];
strcpy(path,this->path.c_str());//此->路径可以不同于当前工作路径
/*如果(chdir(路径)=-1){
perror(“更改工作目录时出错”);
关闭(clie_sock);
出口(1);
}*/
directory=opendir(路径);
struct tm*时钟;
结构统计属性;
结构passwd*pw;
结构组*gr;
弦线;
字符文件_信息[1000];
.....
while((ent=readdir(目录))!=NULL){
line.clear();
统计(ent->d_名称和属性);
时钟=gmtime(&(attrib.st_mtime));
pw=getpwuid(attrib.st_uid);
gr=getgrgid(属性st_gid);
if(S_ISDIR(属性st_模式))
行。追加(1,'d');
else行。追加(1,“-”);
if(属性st_模式和S_IRUSR)
行。追加(1,'r');
else行。追加(1,“-”);
if(attrib.st_模式和S_IWUSR)
行。追加(1,'w');
else行。追加(1,“-”);
if(属性st_模式和S_IXUSR)
行。追加(1,'x');
else行。追加(1,“-”);
if(attrib.st_模式和S_IRGRP)
行。追加(1,'r');
else行。追加(1,“-”);
if(attrib.st_模式和S_IWGRP)
行。追加(1,'w');
else行。追加(1,“-”);
if(属性st_模式和S_IXGRP)
行。追加(1,'x');
else行。追加(1,“-”);
if(属性st_模式和S_IROTH)
行。追加(1,'r');
else行。追加(1,“-”);
if(属性st_模式和S_IWOTH)
行。追加(1,'w');
else行。追加(1,“-”);
if(属性st_模式和S_IXOTH)
行。附加(“x”);
else行。追加(“-”);
sprintf(文件信息,“%s%d%s%s%d%s%d%02d:%02d%s\r\n”,第.c\u str()行,pw->pw\u uid,
pw->pw_name,gr->gr_name,(int)attrib.st_size,getMonth(clock->tm_mon).c_str(),
时钟->tm_-mday,时钟->tm_-hour,时钟->tm_-min,ent->d_-name);
如果(发送(c_数据存储,文件信息,strlen(文件信息),0)=-1){
perror(“书写错误”);
关闭(clie_sock);
出口(1);
}
当你这样做的时候
stat(ent->d_name, &attrib);
您应该记住,ent->d_name
只包含文件名,而不包含完整路径。因此,如果要列出与程序当前目录不同的目录中的文件,则需要构造完整路径以供使用
最简单的解决办法可能是做如下事情
std::string full_path = path;
full_path += '/';
full_path += ent->d_name;
if (stat(full_path.c_str(), &attrib) != -1)
{
// Do your stuff here
}
当你这样做的时候
stat(ent->d_name, &attrib);
您应该记住,ent->d_name
只包含文件名,而不包含完整路径。因此,如果要列出与程序当前目录不同的目录中的文件,则需要构造完整路径以供使用
最简单的解决办法可能是做如下事情
std::string full_path = path;
full_path += '/';
full_path += ent->d_name;
if (stat(full_path.c_str(), &attrib) != -1)
{
// Do your stuff here
}
非常感谢,实际上我的代码中还有一个错误,但现在一切正常。非常感谢,我的代码中还有一个错误,但现在一切正常。