Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Getting file size
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Alex Vinokur  
View profile  
 More options Feb 15 2005, 2:07 am
Newsgroups: alt.sources
From: "Alex Vinokur" <ale...@big-foot.com>
Date: Tue, 15 Feb 2005 09:07:39 +0200
Local: Tues, Feb 15 2005 2:07 am
Subject: [C++] Getting file size
// ==========================================
// Getting file size
// Usage Sample Code
// Version GFS-1.1
// ---------------------------
// Alex Vinokur
//   email: alex DOT vinokur AT gmail DOT com
//   http://up.to/alexvn
// ==========================================

// ==================
#if ((defined unix) || (defined __unix) || (defined unix__) || (defined __unix__))
#if (!(defined __DJGPP__))
#define UNIX_ENV
#endif
#endif

// ------------------
#include <cassert>
#include <string>
#include <iostream>
#include <fstream>
#ifdef UNIX_ENV
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
using namespace std;

#define TXT_FILE_NAME   "foo.txt"
#define BIN_FILE_NAME   "foo.bin"

// -----------------
size_t get_filesize_via_fseek_ftell (
 const char * const filename_i,
 bool               txt_mode_i = true
 )
{
FILE* fp = NULL;

  fp = fopen(filename_i, txt_mode_i ? "r" : "rb");
  assert (fp);

int rc = fseek(fp, 0, SEEK_END);
  assert (rc == 0);

const size_t ret_filesize (ftell(fp));

  rc = fclose(fp);
  assert (rc == 0);

  return ret_filesize;

}

#ifdef UNIX_ENV

// -----------------
size_t get_filesize_via_lseek (
 const char * const filename_i,
 bool               txt_mode_i = true
 )
{
int fd = 0;

  fd = open(filename_i, txt_mode_i ? O_RDONLY : O_RDONLY | O_BINARY);
  assert (fd != -1);

off_t rc;
  rc = lseek(fd, 0, SEEK_END);
  assert (rc != -1);

const size_t ret_filesize (static_cast<size_t>(rc));

  rc = close(fd);
  assert (rc == 0);

  return ret_filesize;

}

// -----------------
size_t get_filesize_via_fstat (
 const char * const filename_i,
 bool               txt_mode_i = true
 )
{
int fd = 0;

  fd = open(filename_i, txt_mode_i ? O_RDONLY : O_RDONLY | O_BINARY);
  assert (fd != -1);

struct stat buf;
int rc = fstat(fd, &buf);
  assert (rc == 0);

const size_t ret_filesize (static_cast<size_t>(buf.st_size));

  rc = close(fd);
  assert (rc == 0);

  return ret_filesize;

}

// -----------------
size_t get_filesize_via_stat (
 const char * const filename_i
 )
{
struct stat buf;

int rc = stat (filename_i, &buf);
  assert (rc == 0);

const size_t ret_filesize (static_cast<size_t>(buf.st_size));

  return ret_filesize;

}

#endif

// -----------------
size_t get_filesize_via_seekg_tellg (
 const char * const filename_i,
 bool               txt_mode_i = true
 )
{
ifstream fs;

  if (txt_mode_i) fs.open (filename_i);
  else            fs.open (filename_i, ios::binary);

  assert (fs);
  assert (fs.is_open());

   fs.seekg(0, ios::beg);
const ios::pos_type start_pos = fs.tellg();

   fs.seekg(0, ios::end);
const ios::pos_type end_pos = fs.tellg();

const size_t ret_filesize (static_cast<size_t>(end_pos - start_pos));

  fs.close();
  assert (!fs.is_open());

  return ret_filesize;

}

// -----------------
size_t get_filesize_via_distance (
 const char * const filename_i,
 bool               txt_mode_i = true
 )
{
ifstream fs;

  if (txt_mode_i) fs.open (filename_i);
  else            fs.open (filename_i, ios::binary);

  assert (fs);
  assert (fs.is_open());

const size_t ret_filesize (static_cast<size_t>(distance(
 istreambuf_iterator<char>(fs),
 istreambuf_iterator<char>()
 )));

  fs.close();
  assert (!fs.is_open());

  return ret_filesize;

}

// -----------------
size_t get_filesize_via_rdbuf_pubseekoff (
 const char * const filename_i,
 bool               txt_mode_i = true
 )
{
ifstream fs;

  if (txt_mode_i) fs.open (filename_i);
  else            fs.open (filename_i, ios::binary);

  assert (fs);
  assert (fs.is_open());

const size_t ret_filesize (static_cast<size_t>(fs.rdbuf()->pubseekoff (0,ios::end,ios::in)));
  fs.rdbuf()->pubseekpos (0,ios::in);

  fs.close();
  assert (!fs.is_open());

  return ret_filesize;

}

// -----------------
void create_file (
 const string&       data_i,
 const char * const  filename_i,
 bool                txt_mode_i = true
 )
{
  remove (filename_i);

ofstream fs;

  if (txt_mode_i) fs.open (filename_i);
  else            fs.open (filename_i, ios::binary);

  assert (fs);
  assert (fs.is_open());

  fs << data_i;

  fs.close();
  assert (!fs.is_open());

}

int main ()
{
const char data[] = "\n";
  create_file (data, TXT_FILE_NAME);
  create_file (data, BIN_FILE_NAME, false);

  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_fseek_ftell" << endl;

  cout << "Created in TXT mode, read in TXT mode: "
       << get_filesize_via_fseek_ftell (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode, read in BIN mode: "
       << get_filesize_via_fseek_ftell (BIN_FILE_NAME, false)
       << endl;

  cout << "Created in TXT mode, read in BIN mode: "
       << get_filesize_via_fseek_ftell (TXT_FILE_NAME, false)
       << endl;

  cout << "Created in BIN mode, read in TXT mode: "
       << get_filesize_via_fseek_ftell (BIN_FILE_NAME)
       << endl;

#ifdef UNIX_ENV
  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_lseek" << endl;

  cout << "Created in TXT mode, read in TXT mode: "
       << get_filesize_via_lseek (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode, read in BIN mode: "
       << get_filesize_via_lseek (BIN_FILE_NAME, false)
       << endl;

  cout << "Created in TXT mode, read in BIN mode: "
       << get_filesize_via_lseek (TXT_FILE_NAME, false)
       << endl;

  cout << "Created in BIN mode, read in TXT mode: "
       << get_filesize_via_lseek (BIN_FILE_NAME)
       << endl;

  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_fstat" << endl;

  cout << "Created in TXT mode, read in TXT mode: "
       << get_filesize_via_fstat (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode, read in BIN mode: "
       << get_filesize_via_fstat (BIN_FILE_NAME, false)
       << endl;

  cout << "Created in TXT mode, read in BIN mode: "
       << get_filesize_via_fstat (TXT_FILE_NAME, false)
       << endl;

  cout << "Created in BIN mode, read in TXT mode: "
       << get_filesize_via_fstat (BIN_FILE_NAME)
       << endl;

  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_stat" << endl;

  cout << "Created in TXT mode                  : "
       << get_filesize_via_stat (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode                  : "
       << get_filesize_via_stat (BIN_FILE_NAME)
       << endl;

#endif

  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_seekg_tellg" << endl;

  cout << "Created in TXT mode, read in TXT mode: "
       << get_filesize_via_seekg_tellg (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode, read in BIN mode: "
       << get_filesize_via_seekg_tellg (BIN_FILE_NAME, false)
       << endl;

  cout << "Created in TXT mode, read in BIN mode: "
       << get_filesize_via_seekg_tellg (TXT_FILE_NAME, false) << endl;

  cout << "Created in BIN mode, read in TXT mode: "
       << get_filesize_via_seekg_tellg (BIN_FILE_NAME)
       << endl;

  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_distance" << endl;

  cout << "Created in TXT mode, read in TXT mode: "
       << get_filesize_via_distance (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode, read in BIN mode: "
       << get_filesize_via_distance (BIN_FILE_NAME, false)
       << endl;

  cout << "Created in TXT mode, read in BIN mode: "
       << get_filesize_via_distance (TXT_FILE_NAME, false)
       << endl;

  cout << "Created in BIN mode, read in TXT mode: "
       << get_filesize_via_distance (BIN_FILE_NAME)
       << endl;

  // -------------------------------------------
  cout << endl;

  cout << "--- get_filesize_via_rdbuf_pubseekoff" << endl;

  cout << "Created in TXT mode, read in TXT mode: "
       << get_filesize_via_rdbuf_pubseekoff (TXT_FILE_NAME)
       << endl;

  cout << "Created in BIN mode, read in BIN mode: "
       << get_filesize_via_rdbuf_pubseekoff (BIN_FILE_NAME, false)
       << endl;

  cout << "Created in TXT mode, read in BIN mode: "
       << get_filesize_via_rdbuf_pubseekoff (TXT_FILE_NAME, false)
       << endl;

  cout << "Created in BIN mode, read in TXT mode: "
       << get_filesize_via_rdbuf_pubseekoff (BIN_FILE_NAME)
       << endl;
  return 0;

}

--
 Alex Vinokur
     email: alex DOT vinokur AT gmail DOT com
     http://mathforum.org/library/view/10978.html
     http://sourceforge.net/users/alexvn

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google