【问题标题】:How can calculate the size of any file in C++ or Visual C++/.NET or C#? [duplicate]如何在 C++ 或 Visual C++/.NET 或 C# 中计算任何文件的大小? [复制]
【发布时间】:2011-03-03 19:39:34
【问题描述】:

可能重复:
How do you get the file size in C#?

如何在 C++ 或 Visual C++/.NETC# 中计算任何文件的大小?

我的意思是 .exe、.txt、.doc 等文件。

【问题讨论】:

    标签: c# c++ visual-c++


    【解决方案1】:

    “计算”是什么意思?

    您只需询问文件系统,“那个文件有多大?”它会告诉您长度,不涉及计算。

    你的问题到底是什么?

    如何在 C# 中做到这一点?

    这里的代码会给你一个文件的长度,还有其他方法:

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

    【讨论】:

      【解决方案2】:

      你可以在 C++ 中这样做:

      直接取自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;
      }
      

      【讨论】:

      • 不需要第一个tellg和(end - begin)语句,只需打印end。
      • 是的,我想他们不需要。
      【解决方案3】:

      如果你只想要文件的大小,你可以在 C# 中这样做:

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

      【讨论】:

      • 我在 Ip 数据包中隐藏数据我将把文件分成我想知道长度的位
      【解决方案4】:

      另一个符合您要求的变体——使用 Windows 文件管理 API 的 Visual C++:

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

      【讨论】:

        【解决方案5】:

        使用统计数据。

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

        【讨论】:

        • 这既不是 C++、Visual C++ 也不是 C#。
        • 它非常简单,我在我的 c++ 编程中使用它来检索文件大小。我的 ftp 客户端完全依赖它,而且效果很好。
        猜你喜欢
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 2017-06-29
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 2011-08-15
        相关资源
        最近更新 更多