开发者

How can calculate the size of any file in C++ or Visual C++/.NET or C#? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How do you get the 开发者_高级运维file size in C#?

How can I calculate the size of any file in C++ or Visual C++/.NET or C#?

I mean a .exe, .txt, .doc, etc. file.


What do you mean "calculate"?

You just ask the file system, "how big is that file?", and it gives you the length, no calculation involved.

What is your question exactly?

How to do that in C#?

Here's code that will give you the length of a file, there are other ways as well:

long length = new FileInfo(@"c:\temp\test.exe").Length;


You can do it in C++ this way:

Taken directly from cplusplus.com

// 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;
}


If you just want the size of the File, you can do this in C#:

long size = new System.IO.FileInfo("<path to file>").Length;


Another variant that matches your request -- Visual C++ using Windows File Management API:

LARGE_INTEGER liFileSize;
HANDLE hFile;
// open/create file 
GetFileSizeEx(hFile, &liFileSize);
// close handle


Use stat.

struct stat buf; fstat(fd, &buf); int size = buf.st_size;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜