【发布时间】:2017-06-01 06:33:03
【问题描述】:
我是 C++ 编程新手,我正在尝试练习文件读写。我正在尝试获取当前目录的所有文件的大小。问题是,在获取当前目录中的文件名后,我将它们放在一个文本文件中。所以现在我被困住了,不知道从哪里开始。
#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;
}
【问题讨论】:
-
你把它们放在一个文本文件中然后......?接下来你想做什么?
-
将这些文件放入文本文件后,我还希望能够输出这些文件的大小。
-
fileContents是其中一个文件的名称。打开它并计算字符数。