C++ size of a file

To calculate a size in byte of a file, one solution is counting the number of characters in the file. One character is equal to 1 byte.
Example:
#include<cmath>
#include<iostream>
#include<fstream>
using namespace std;
void writetofile(char *filename){
   char *str="This is the content of the file.\n";
   fstream fout;
   fout.open(filename,ios::out);
   if(!fout){
         cout<<"Could not open the file!";
         return;
         }
   while(*str) fout.put(*str++);
   cout<<*str;
   fout.close();
  
   }
void calfilesize(char *filename){
   char ch;
   int fsize=0;
   ifstream in(filename,ios::in);
   if(!in){
         cout<<"Could not open the file!"<<"\n";
         return;
         }
   cout<<"\nFILE CONTENT:\n";
   while(in.get(ch)) {
               cout<<ch;
               fsize++;
               }
              
                
   cout<<"\nFILE SIZE:"<<fsize<<" bytes"<<"\n";
  
   in.close();
  
   }

int main(){
   writetofile("D:\\testfile.txt");
   calfilesize("D:\\testfile.txt");
   system("PAUSE");
   return 0;
}

Posted by: Dara | post date: 02-12-2013 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.