Reading binary file size and copying it to a different folder

I am creating an executable C++ program that will automatically read the size of a file and if it stops changing, it will copy and move that file to a different folder.  I am relatively new at programming so am not sure how to pursue this program.  The file is a binary (.hst) file.  Thank you in advance.

Also I am using Microsoft Visual Studio 2010 and with it I am using C++ Win32 console development platform.
Sara WilsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Have you created anything yet?  If this is a first time creating a C++ application in VS, this is a little more complex than I would have selected.

Approach it modularly, think about what chunks of functionality and code you will need and try to get some of those working independently.

Based on what you are describing I see several obvious things that you will need to design and code:
  1. Get the length of a file
  2. Some sort of timer driven loop that can check the file every so many minutes, etc
  3. Copy/move the file to a different folder

So perhaps you start with a simple one like "how do I get the current size of a file?"  Once you get that working, you will have learned a bit more and you tackle the next one.

This is a little bigger than I generally write as a solution to a question, and I expect you want to do a lot of this yourself to learn from it, which is great.  So see if you can find a "Hello world" example on the interwebs and get that working, then add some logic to get the size of a file.  Something like the following might get you started in the .Net world.

Sara WilsonAuthor Commented:
I bought the book Sams Teach yourself C++ by Siddhartha Rao and I have completed it till chapter 8.  In chapter 27 they teach how to use streams for input and output (so this chapter is still very advance for me). I am currently trying to find an example that will help me select a file of my choice and determine its length but I haven't been successful so far.  I definitely had the approach you stated in mind for me to get this to work.  I will give it a try and see if I can get it working using the link you suggested.  Thank you so much for your input I really appreciate it.
Fabrice LambertFabrice LambertCommented:
If you can, I suggest to upgrade to the last version of Visual Studio, since it support some very nice features that will help your developments a lot, such as filesystem library, chrono library, smart pointers (unique_ptr, shared_ptr), ownership and move semantic ect ect ...

Alternatively, you can install the boost Library via NuGet (the package manager Embedded within VS) wich will provide the libraries / class mentionned above.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

I found the following code from a pretty reliable source. It shows you how to determine the size of a file.

// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  streampos begin,end;
  ifstream myfile ("example.bin", ios::binary);
  begin = myfile.tellg();
  myfile.seekg (0, ios::end);
  end = myfile.tellg();
  cout << "size is: " << (end-begin) << " bytes.\n";
  return 0;

Open in new window

More info: tellg
More info: seekg

Be sure to bookmark this and keep it in your favorite's toolbar:
The link you posted doesn't look like ISO C++. Correct me if I'm wrong, but it looks to me like a non-portable Microsoft managed C++ version. In fact, the link has the words, ".NET Framework".

The book description refers to standard C++, which refers to the ISO C++ Standard.
>> and if it stops changing
You better have some better requirements, since conceivably, if another program adds one byte at a time, say, once every few seconds, then your program could run for years until your diskspace runs out. If you don't have better requirements to cover this point, then you should make some reasonable assumptions.
Fabrice LambertFabrice LambertCommented:
The book description refers to standard C++, which refers to the ISO C++ Standard. 
Hmm, by looking at the content, a book starting with C-style arrays, raw pointers before Standard Library, speaking about exceptions handling until very late (when it is tied to raw pointers), neglecting class semantic can't be a good one.

Raw pointers (and C-style arrays who usually decay to raw pointers) have many issues (safety, validity, ownership, exceptions ect ...) that are solved by Standard Library containers and smart pointers. Better learn that first.
>> I am relatively new at programming ...
>>  a book starting with C-style arrays, raw pointers before Standard Library, speaking about exceptions handling until very late (when it is tied to raw pointers), neglecting class semantic can't be a good one.

So what book do you recommend for this beginner?
the easiest way to get file size in c and c++ is to using the stat function.

#include <sys/stat.h>


size_t getFileSize(const char * szFullPathToFile)
        struct stat fileStatus = { 0 };
        if (stat(szFullPathToFile, &fileStatus) != 0)
              // file or directory doesn't exist 
              return 0;
        return fileStatus.st_size;

Open in new window

the stat  function also should work if the file is still open by others. so you could call the stat function twice with a little delay between, and if the file size didn't change, the chance that it was released by another application is much higher.

note, a file which is opened for write by another application, normally cannot be opened by your program since access was denied. so you could try in a polling loop (each cycle do a little wait for a few ms) whether you can rename it. you aland that the rename succeeds is higher.  

to copy the file you could/should use system copy functions which your operation system or your IDE provides.

if you want to copy yourself, you should open the renamed source file in read and binary mode. open the destination file in binary mode with write access. if both files are successfully opened, read from the source file in chunks, say 64k blocks, up to the size, which you retrieved from the getFileSize function. after each read write the block to the destination file (and undo the renaming if required).


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.