[英]C++: Getting size of all files inside current directory
I'm new to C++ programming, and I'm trying to practice file reading and writing. 我是C ++编程的新手,并且正在尝试练习文件读写。 I'm trying to get the sizes of all the files of the current directory. 我正在尝试获取当前目录中所有文件的大小。 Thing is, after getting the names of the files in the current directory, I place them inside of a text file. 问题是,在获得当前目录中文件的名称之后,我将它们放置在文本文件中。 So now I'm stuck, and don't know where to go from here. 所以现在我被困住了,不知道从这里去哪里。
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
// FILE FUNCTION
void fileStuff(){
}
// MAIN FUNCTION
int main(int argc, char const *argv[])
{
// ERROR CHECKING
if(argc != 3){ // IF USER DOESN'T TYPE ./nameOfFile, AND THE OTHER REQUIRED ARGUMENTS.
cout << "Incorrect. Try Again" << endl;
exit(-1);
}
ifstream file;
string fileContents;
system("find . -type f > temp.txt");
file.open("temp.txt");
if (!file){
cout << "Unable to open file: temp.txt" << endl;
exit(-1);
}
while(file){
getline(file, fileContents);
cout << fileContents << endl;
}
file.close();
return 0;
}
C++14 (and earlier versions, notably C++11 ) does not know about file systems and directories (yet). C ++ 14 (以及更早的版本,尤其是C ++ 11 )尚不了解文件系统和目录 。 For C++17, see its file system library . 对于C ++ 17,请参阅其文件系统库 。 Otherwise, your code is operating system specific, but Boost library has some file system support . 否则,您的代码是特定于操作系统的,但是Boost库具有某些文件系统支持 。
I am assuming you are running on Linux or some POSIX system. 我假设您正在Linux或某些POSIX系统上运行。
Your program just uses an external command ( find(1) ); 您的程序仅使用外部命令( find(1) ); if you want to read from such a command, you might use popen(3) with pclose
, then you won't need a temporary file. 如果您想从这样的命令中读取pclose
,则可以将popen(3)与pclose
,则不需要临时文件。 BTW, you could use find . -type f -ls
顺便说一句,您可以使用find . -type f -ls
find . -type f -ls
. find . -type f -ls
。
However, you don't need to use an external command, and it is safer (and faster) to avoid that. 但是,您不需要使用外部命令,这样可以更安全(更快速)地避免这种情况。
Pedantically, a file name could contain a newline character, and with your approach you'll need to special case that. 徒劳地,文件名可以包含换行符,使用这种方法,您需要对此进行特殊处理。 A file name could also contain a tab character (or other control characters) and in that case find . -type f
文件名也可以包含制表符(或其他控制字符),在这种情况下, find . -type f
find . -type f
behave specifically, and you would also need to special case. find . -type f
行为特别明确,您还需要特殊情况。 In practice, it is extremely poor taste and very unlikely to have a newline or tab character in a file name and you might forget these weird cases. 实际上,它的味道极差,并且在文件名中不太可能包含换行符或制表符,您可能会忘记这些奇怪的情况。
You could use nftw(3) . 您可以使用nftw(3) 。 You could recursively use opendir(3) & loop on readdir(3) (and later closedir
). 您可以递归地使用opendir(3)和readdir(3)上的循环(以及后来的closedir
)。
Once you have a file path, you would use stat(2) to get that file's metadata, including its size (field st_size
). 一旦有了文件路径,就可以使用stat(2)来获取该文件的元数据,包括其大小 (字段st_size
)。 BTW the /bin/ls
and /usr/bin/find
programs use that. 顺便说一句, /bin/ls
和/usr/bin/find
程序使用它。
The readdir(3) function returns a struct dirent
pointer ending with d_name
; readdir(3)函数返回以d_name
结尾的struct dirent
指针; you probably want to skip the two entries for .
您可能要跳过的两个条目.
and ..
(so use strcmp(3) to compare with "."
and ".."
, or do the compare the hard way). 和..
(因此,请使用strcmp(3)与"."
和".."
进行比较,或者以比较的方式进行比较)。 Then you'll build a complete file path using string catenation. 然后,您将使用字符串分类构建完整的文件路径。 You might use (in genuine C++) std::string or you could use snprintf(3) or asprintf(3) for that. 您可以使用(在正版C ++中) std :: string或为此使用snprintf(3)或asprintf(3) 。 If you readdir
the current directory .
如果您readdir
目录当前目录.
you could call stat(2) directly on d_name
field. 您可以直接在d_name
字段上调用stat(2) 。
BTW exit(-1)
is incorrect (and certainly poor taste). BTW exit(-1)
不正确(而且味道也很差)。 See exit(3) . 参见exit(3) 。 A much more readable alternative is exit(EXIT_FAILURE)
更具可读性的替代方法是exit(EXIT_FAILURE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.