Connecting Tech Pros Worldwide Forums | Help | Site Map

File Size

Member
 
Join Date: Jun 2006
Posts: 57
#1: Jun 29 '06
How Can I Print The File Size In Console??????(in Bytes)

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#2: Jun 29 '06

re: File Size


I don't quite understand how what you are asking this time is different from the discussion you started in this thread

http://www.thescripts.com/forum/thread506204.html
Newbie
 
Join Date: Jun 2006
Posts: 2
#3: Jun 29 '06

re: File Size


Finding the size of the file in C/C++ can be done easily.

Create a file pointer for the input file.
Read character by character and count the characters in a variable.
The total no. of characters in count gives the size of the file in bytes.

Try it.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#4: Jun 29 '06

re: File Size


Quote:

Originally Posted by kuttalambala

Finding the size of the file in C/C++ can be done easily.

Create a file pointer for the input file.
Read character by character and count the characters in a variable.
The total no. of characters in count gives the size of the file in bytes.

That's a rather long winded method when you can open a file,jump to the end of it and the read the current file pointer position (i.e. the file size) without having to read every character and keep your own count.

I suggest you look at the other thread I have linked to in my first reply.
Member
 
Join Date: Jun 2006
Posts: 57
#5: Jun 30 '06

re: File Size


Thanks!!!!!!! But I don't Understand One Thing:

If Files Characters are 10400 Will The File Be 10400 bytes??????
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,169
#6: Jun 30 '06

re: File Size


Well that rather depends, if you used kuttalambala method and you opened the file in text then the number of characters wont neccessarily be the number of bytes because end of line processing happens which means that the character sequence in the file "\r\n" (carridge return newline) is returned to the program as "\n" so every time there is a new line in the file you count 1 less characters than actually exist in it (for a file with DOS/Windows end of lines).

However ignoring that then basically file byte size and number of characters in the file are basically the same.
Member
 
Join Date: Jun 2006
Posts: 57
#7: Jun 30 '06

re: File Size


Thanks!!!!
Newbie
 
Join Date: Aug 2007
Posts: 2
#8: Aug 20 '07

re: File Size


Why not stick to the old faithful ? :

Expand|Select|Wrap|Line Numbers
  1. long FileSize(char* filename)
  2. {
  3.   struct stat stbuf;
  4.   stat(filename, &stbuf);
  5.   return stbuf.st_size;
  6. }
That's the method described in "The C Programming Language" is much simpler than any other technique, plus you get access to all this as well:

Expand|Select|Wrap|Line Numbers
  1. struct stat   /* inode information returned by stat */
  2.    {
  3.        dev_t     st_dev;      /* device of inode */
  4.        ino_t     st_ino;      /* inode number */
  5.        short     st_mode;     /* mode bits */
  6.        short     st_nlink;    /* number of links to file */
  7.        short     st_uid;      /* owners user id */
  8.        short     st_gid;      /* owners group id */
  9.        dev_t     st_rdev;     /* for special files */
  10.        off_t     st_size;     /* file size in characters */
  11.        time_t    st_atime;    /* time last accessed */
  12.        time_t    st_mtime;    /* time last modified */
  13.        time_t    st_ctime;    /* time originally created */
  14.    };
  15.  
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#9: Aug 20 '07

re: File Size


Quote:

Originally Posted by steveski74

Why not stick to the old faithful ? :

It's not portable. Only works for Unix.
Newbie
 
Join Date: Mar 2007
Posts: 9
#10: Nov 5 '08

re: File Size


cross-platform:

Expand|Select|Wrap|Line Numbers
  1.   ifstream is;
  2.   is.open ("test.txt", ios::binary );
  3.  
  4.   // get length of file:
  5.   is.seekg (0, ios::end);
cheers,
lini
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#11: Nov 5 '08

re: File Size


That's fine, as long as you remember that stat() isn't part of the Standard C Library so it may not be fully portable. However, stat is part of IEEE Std 1003.1 (POSIX) so it should be available in at least all versions of unix.
Reply