使用stat列出文件及其信息
List files and their info using stat
我正在使用c++编程ftp服务器,我需要能够以以下形式获得有关文件的所有信息:
sent: drwxr-xr-x 1000 ubuntu ubuntu 4096 May 16 11:44 Package-Debug.bash
所以我可以把它发送给客户端。我部分成功地做到了这一点,但我遇到了一些问题。下面是我的一部分代码:
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 %srn", 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;
}
.....
}
当path变量与当前工作路径不同时,此代码不起作用。Valgrind说,有许多跳转依赖于未初始化的值等,文件列表包含错误的值-只有文件名和大小是正确的。当我将当前工作目录更改为path变量的内容时,它没有报告任何错误,但文件列表仍然包含错误信息。我真的不知道我的代码出了什么问题,所以非常感谢任何帮助。
当你这么做的时候
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
}
相关文章:
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 创建进行生产构建并创建调试信息的C++生成文件的最佳方法?
- 将对象信息保存到二进制文件中
- 错误 VSP1011:无法获取调试信息.与 /配置文件链接器开关链接
- Qt读取文本文件的一些信息
- 将文件信息(字符数组)嵌入到编译的二进制文件中
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件
- 是否可以将信息从XML文件转换为CPP
- 如何在文本文件中搜索员工记录(按名称)并仅显示其详细信息?
- 使用TDD时隐藏文件访问实现详细信息
- 生成有关编译的信息文件
- 如果我尝试将对话框从一个项目移动到另一个项目,我是否需要从 rc 文件中复制 DESIGNINFO、对话框信息和AFX_
- 在远程嵌入式设备上使用核心文件的 GDB - 如何获取有关回溯的更多信息?
- 接受多个输入(如 +、- 和平方数字)的计算器.从文本文件中提取信息
- 如何在 Windows 上从 C++11 统计信息对象的文件序列号中查找文件名
- 构建 vcxproj 文件给我一个无信息的错误"The system cannot find the path specified"
- 什么是阅读和将信息与此文本文件分开的好方法
- Fuse文件系统查找/Trash、/autorun.inf和/.xdg卷信息文件
- DWARF调试信息文件的diff工具