I have this program where I'm trying to find the file size with the boost library. I find the files fine, but it won't get the file's size, where am I wrong? Thanx for your help...

Here's my code so far.
It compiles fine, but won't return the file size.

#include <boost/filesystem.hpp>
namespace bf = boost::filesystem;

#include <iostream>
#include <stdio.h>
using namespace std;

int main( int argc, char* argv[] ) {

	bf::path fullpath( bf::initial_path() );//create new filesystem path

	if( argc > 1 )
		fullpath = bf::system_complete(bf::path( argv[1], bf::native ) );//turns given filename into reference 
	                                                                     //and sees if it is sctually there.
	
	if( !bf::exists( fullpath ) ) {
		cout << "Could not find: " << fullpath.native_file_string() << endl;//returns in native format
	}//end if
	
	if( bf::is_directory( fullpath ) ) {

		cout << "In directory: " << fullpath << endl;

		bf::directory_iterator dirIter( fullpath );//recursive_directory looks into folder/next folder/ ect...
		bf::directory_iterator endIter;

		for( ; dirIter != endIter; ++dirIter ) {

			//trap file or folder name your not allowed to read
			try {
				cout << dirIter->filename() << endl;
			} catch( std::exception const& ex ) {
				cerr << dirIter->filename() << " " << ex.what() << endl;
			}//end catch
		}//end for
	}//end if

	else {
		cout << "\Found: " << fullpath.native_file_string() << endl;
	}//end if else
	
	cout << fullpath << endl;



//check file size
if ( argc != 2 ) {
    cout << "usage: file_size path\n";
    return 1;
  }//end if

  // print out size of intmax_t
  cout << "sizeof(intmax_t) is " << sizeof(boost::intmax_t) << '\n';

  //error checking...
  if ( !bf::exists( fullpath ) ) {                            
		cout << "not found: " << argv[1] << endl;
		return 1;
	}//end if

  if ( !bf::is_regular( fullpath ) ) {                         
			cout << "not a regular file: " << fullpath.native_file_string() << endl;
			return 1;
		}//end if
 
    cout << "size of " << argv[1] << " is " << bf::file_size( fullpath ) << endl;
    return 1;


}//end main

Recommended Answers

All 22 Replies

Seek to the end of the file and call tellg(), or whatever function boost used to return the current file position.

I didn't think this boost file_size function needed a seek.

When I run this code, the error "is not a regular file" comes up. That would be the if ( !bf::is_regular( fullpath ) ) error check.

I don't even know what is_regular means, which is probably bad :)

I don't use boost -- just as easy to use fstream which is standard on all (or most) platforms that have file systems and c++. I see no point in using boost to replace fstream.

This is kind of a school thing where we're supposed to learn to use boost, otherwise I wouldn't be using it...

Did you see this example?

I see boost::filesystem has at least one redeeming value -- a platform independent way to get a list of all the files and directories.

Yeah, that's basically what I have in my program, but i changed the parameters to match the rest of my program...

As far as I understand, this line of code is supposed to get the file size:

cout << "size of " << argv[1] << " is " << bf::file_size( fullpath ) << endl;

But it doesn't.
Anybody that understands this boost file_size function, I'd greatly appreciate your help. Thank-you.

Ok, I changed the project debugging command arguments down to the exact file I wanted, and it did give me the size. (visual studio 2008)

But how do I go thru A folder and get all the files size's that are inside?

> But how do I go thru A folder and get all the files size's that are inside?

See: http://www.daniweb.com/forums/post342872.html#post342872

That just prints the native_file_string. To get the file size, construct a boost::filesystem::path from iter->native_file_string() and then use boost::filesystem::file_size on it, as in your current code.

You have to iterate through all the files in the folder and sum them up. See the link I gave you earlier because boost has a function to do that.

[edit]^^^ beat me to it :)

Thanx for the reply.

Tho I'm not sure I understand...

I can list all the files in a directory. Or get a specific file size if i give it a specific file in a directory. So can't I just loop thru the directory and print out the size everytime it finds a file?

I know these questions are newbie, but that's kinda what I am. Thanx for your patience...

Thanx for the reply.

Tho I'm not sure I understand...

I can list all the files in a directory. Or get a specific file size if i give it a specific file in a directory. So can't I just loop thru the directory and print out the size everytime it finds a file?

.

You mean you don't know how to put the two together so that you can get the size of all the files in a directory? Odd.:-O

Well, that's what I thought I was doing In my first program.

Then I tries putting it in the for loop I have, but that didn't work either...

you put it in the wrong place. Check the file size on line 39 (inside the else statement). It would make your program a lot easier to follow if you wrote another function to return the file size.

Putting that file_size line of code on line 39,in the else statement like you said, works for a specific file again, but not if I give it a directory with a bunch of folders and files in it...

I'm starting to not like this program :)

You have to write the code that iterates through all the files in the folder and sum up their sizes. boost library doesn't do that for you. I could easily write such a program without boost, but I doubt that is what you want. You need to write a function that iterates through all the files and directories, then when a directory is found call that function recursively to process all the files in the sub-directory. I posted a code snippet a couple years or so ago that does just that using win32 api functions.

Ok, Well, I'll play around with it. Thanx for your guys help and patience...

See, here is an output I want it to be like...

Should give you a better idea of what I'm looking for.

Scanning: c:/usr/Solutions/curriculum/C++/fileusage/fileusage
Ext : # : Total
---------- : ---- : -----------
.cpp : 1 : 5,342
.dep : 2 : 124
.htm : 2 : 14,428
.idb : 2 : 1,972,224
.manifest : 5 : 3,837
.obj : 2 : 4,163,449
.pdb : 2 : 1,441,792
.res : 2 : 2,584
.user : 1 : 1,442
.vcproj : 1 : 4,024
---------- : ---- : -----------
10 : 20 : 7,609,246

Use the <map> class to create a list of file extensions and file sizes. As the files are read then update the map. When done just display the map contents. boost will not do that for you, you have to code it yourself.

I was thinking of maybe an advanced vector, but, same idea right?
Cause there we can store multiple info in one element...
Tho I haven't tried it yet...

Yes you could use a vector of structures. The structure just has two members: string extension and int count.

Ok, so how would I get just the file extension from the file name and store that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.