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:
#include "stdafx.h"
#include <iostream>
#include <fstream>
usingnamespace std;
// obtaining file size
constchar * 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 :)
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.