User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,607 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 539 | Replies: 4 | Solved
Reply
Join Date: Feb 2008
Posts: 445
Reputation: Jennifer84 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro in Training

Check the size of a file

  #1  
May 9th, 2008
I am trying to check the size of a file with this code.
The file is named "OneFile.txt" and is of the size 230 kb.

The MessageBox will show the number 37. If I change the file so it will be 460 kb instead, still the messageBox will show the number 37.
I am not sure if I have missed something here ? I beleive I am not checking the size of the file below.
I think I checking the string ?

 std::string path = "";

path = "C:\\Folder1\\Folder2\\OneFile.txt";
double filesize = path.size();

stringstream Getsize;
std::string Getsize2;
Getsize << filesize;
Getsize2 = Getsize.str();
					 
String^ sizeee = gcnew String(Getsize2.c_str());
MessageBox::Show(sizeee);
Last edited by Jennifer84 : May 9th, 2008 at 5:56 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 877
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Check the size of a file

  #2  
May 9th, 2008
1) file size is an unsigned integer not a double.

2) >> double filesize = path.size();
All that is doing is getting the number of characters in the string, not the file size

Here is how to get the file size
size_t fileSize = 0;
string path = "C:\\Folder1\\Folder2\\OneFile.txt";
ifstream infile(path.c_str());
if(infile.is_open())
{
    infile.seekg(0, ios::end ); // move to end of file
    fileSize = infile.tellg();
}
cout << "file size is " << fileSize << "\n";

There are a couple other ways to get it, such as calling the stat() function.
Last edited by Ancient Dragon : May 9th, 2008 at 6:17 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Feb 2008
Posts: 445
Reputation: Jennifer84 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro in Training

Re: Check the size of a file

  #3  
May 9th, 2008
I understand. This is the first time I am trying to do something like this.
I have rewritten this code a little bit as I am writing this for a buttonControl.
I have taken if(infile.is_open()) away as seen because when having this I received the size of: 0. I am not sure if this was okay to do ?

My file is 230 kb.
The messageBox will show this numer: 4294967295
Have I missed something ?

size_t fileSize = 0;
string path = string path = "C:\\Folder1\\Folder2\\OneFile.txt";

ifstream infile(path.c_str());

//if(infile.is_open())
//{
         infile.seekg(0, ios::end); //move to end of file
         fileSize = infile.tellg();
//}
			
	stringstream Getsize;
	std::string Getsize2;
	Getsize << fileSize;
	Getsize2 = Getsize.str();
					 
	String^ sizeee = gcnew String(Getsize2.c_str());
	MessageBox::Show(sizeee);
Last edited by Jennifer84 : May 9th, 2008 at 6:42 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,691
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 877
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Check the size of a file

  #4  
May 9th, 2008
>>I have taken if(infile.is_open()) away as seen because when having this I received the size of: 0. I am not sure if this was okay to do ?

Don't do that. If is_open() fails that means the file could not be opened for some reason. And in that case all other fstream functions will fail too. You need to find out why the file can not be opened.

>>Getsize2 = Getsize.str();
You don't need Getsize2 string.
String^ sizeee = gcnew String(GetSize.str());
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Feb 2008
Posts: 445
Reputation: Jennifer84 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro in Training

Re: Check the size of a file

  #5  
May 9th, 2008
Yes ofcourse, I forgot to write .txt in the end of the path. I did it in the post but not in my code.
Now it works.

string path = string path = "C:\\Folder1\\Folder2\\OneFile.txt";
Last edited by Jennifer84 : May 9th, 2008 at 7:55 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC