C + + multiple methods to get file size

Source: Internet
Author: User
Tags file size int size

C + + multiple methods to get file size

#include <iostream>
#include <io.h>
#include <sys\stat.h>
#include <afx.h
#define _afxdll
using namespace std;
Void Main ()
{
//This file is inaccessible under Project open
char* filepath = ". \\TEST.NCB ";
//Method one
struct _stat info
_stat (filepath, &info);
int size = info.st_size;
COUT<<SIZE<&L T;endl;
//Method Two
file* FILE = fopen (filepath, "RB");
if (file)
{
int size = filelength (Fileno (file));
cout& lt;<size<<endl;
fclose (file);
}
//Method three
CFile CFile
if (CFile. Open (filepath, cfile::moderead))
{
int size = CFile. GetLength ();
cout<<size<<endl;
}
//Method four
HANDLE HANDLE = CreateFile (filepath, File_read_ea, File_share_read, 0, open_existing, 0, 0);
If (Handle!= Invalid_handle_value)
{
int size = GetFileSize (handle, NULL);
Cout<<size<<endl;
CloseHandle (handle);
}
}

You can also call the WINAPI function directly getfilesize

DWORD WINAPI GetFileSize(
__in HANDLE hFile,
__out LPDWORD lpFileSizeHigh
);

Support for Win2000 and future systems

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.