【发布时间】:2009-07-21 03:12:12
【问题描述】:
我正在学习 C++,然后我正在寻找一些代码来学习我喜欢的领域:文件 I/O,但我想知道我如何调整我的代码以供用户键入他想查看的文件,例如在 wget 中,但我的程序如下:
C:\> FileSize test.txt
我的程序代码在这里:
// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long begin,end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
谢谢!
【问题讨论】:
-
我知道 Stackoverflow 对所有人开放,因此可以免费交换信息,但您会问很多问题,只需简单的 google 搜索即可回答。
-
我之前在谷歌搜索过!
-
在这种情况下建议你使用 stat 函数来获取文件大小。如果成功,它会填写“struct stat”,然后您可以 st_size 来检查文件大小的值。上面的代码没有检查文件是否不存在。无论如何,只是很挑剔......重点是打开从命令行传入的文件名:)