By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

File Size

P: 57
How Can I Print The File Size In Console??????(in Bytes)
Jun 29 '06 #1
Share this Question
Share on Google+
10 Replies


Banfa
Expert Mod 5K+
P: 8,916
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
Jun 29 '06 #2

P: 2
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.
Jun 29 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Jun 29 '06 #4

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

If Files Characters are 10400 Will The File Be 10400 bytes??????
Jun 30 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
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.
Jun 30 '06 #6

P: 57
Thanks!!!!
Jun 30 '06 #7

P: 2
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.  
Aug 20 '07 #8

weaknessforcats
Expert Mod 5K+
P: 9,197
Why not stick to the old faithful ? :
It's not portable. Only works for Unix.
Aug 20 '07 #9

P: 12
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
Nov 5 '08 #10

Expert 100+
P: 2,402
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.
Nov 5 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.