Getting file sizes from a folder

Hi, trying to teach myself c++ by basically finding working code for things I need and then breaking it down. For this I am currently just using a console application on windows using visual studio. I am determining filesize of MKV files which is working fine using this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;



// obtaining file size 

const char * filename = "test.mkv"; 

int main () 
{ 
	double l,m; 
		ifstream file (filename, ios::in|ios::binary); 
		l = file.tellg(); 
		file.seekg (0, ios::end); 
		m = file.tellg(); 
		file.close(); 
		cout << "size of the MKV is "; 
		cout << " is " << (m-l) << " bytes.\n"; 
	system("PAUSE");
return 0; 
}


The code I found was using long variable for l+m but for MKV files which are alot bigger than random files this may be used for I used a double. I have a couple of questions, how can I display in KB do I just use (m-1 / 1024)? if this is the case how do I write the expression in c++ ?

I would then also like to be able to list all files within a folder and then say the file size next to them so instead of a global variable declaring the specific file I can just specify a folder name and then list everything in that with specific file sizes.

Any suggestions are welcome, and please remember I really am the definition of beginner so any pasted code please dont make the assumption I will have a clue what any of it does :)

Thanks,

Ben.
my main suggestion is that if you're going to use c++, don't try and use C stuff.

for example:
use std::string for your filename. (well, just string seeing as you're using the std namespace).

consider the boost library for looking for files in folders (http://www.boost.org/), although that might be heavyweight for your application.
There's no point in using type double for the variables as the tellg() function itself can limit the size of the result.

One thing which I would suggest is to put the code to get the file size into its own separate function like this, then it can be called as required with the filename as input.
1
2
3
4
5
6
long fileLength (const char * name)
{
	ifstream file (filename, ios::in|ios::binary);
	file.seekg (0, ios::end);
	return file.tellg();
}


However, if you are using windows, you may find the windows API functions are better, there is an example here: http://stackoverflow.com/questions/8991192/check-filesize-without-opening-file-in-c

Here's an example using both methods. If the file size is very large, the tellg version will probably fail.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

using namespace std;

 const char * filename = "test.mkv";

long fileLength (const char * name);
__int64 FileSize(std::string name);

int main()
{
    cout << "size of the file is " << fileLength(filename) << " bytes.\n";
    cout << "Size of " << filename << " is " << FileSize(filename) << " bytes." << endl;
    
    return 0;
}

long fileLength (const char * name)
{
    ifstream file (filename, ios::in|ios::binary);
    file.seekg (0, ios::end);
    return file.tellg();
}

__int64 FileSize(std::string name)
{
    HANDLE hFile = CreateFile(name.c_str(), GENERIC_READ,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile==INVALID_HANDLE_VALUE)
        return -1; // error condition, could call GetLastError to find out more

    LARGE_INTEGER size;
    if (!GetFileSizeEx(hFile, &size))
    {
        CloseHandle(hFile);
        return -1; // error condition, could call GetLastError to find out more
    }

    CloseHandle(hFile);
    return size.QuadPart;
}



Actually, depending on the compiler (and maybe operating system), this may work. Note the type long long
1
2
3
4
5
6
long long fileLength (const char * name)
{
	ifstream file (filename, ios::in|ios::binary);
	file.seekg (0, ios::end);
	return file.tellg();
}
Last edited on
Topic archived. No new replies allowed.