File Size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kasya
    New Member
    • Jun 2006
    • 57

    File Size

    How Can I Print The File Size In Console??????(i n Bytes)
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

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

    Comment

    • kuttalambala
      New Member
      • Jun 2006
      • 2

      #3
      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.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • Kasya
          New Member
          • Jun 2006
          • 57

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

          If Files Characters are 10400 Will The File Be 10400 bytes??????

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            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.

            Comment

            • Kasya
              New Member
              • Jun 2006
              • 57

              #7
              Thanks!!!!

              Comment

              • steveski74
                New Member
                • Aug 2007
                • 2

                #8
                Why not stick to the old faithful ? :

                Code:
                long FileSize(char* filename)
                {
                  struct stat stbuf;
                  stat(filename, &stbuf);
                  return stbuf.st_size;
                }
                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:

                Code:
                struct stat   /* inode information returned by stat */
                   {
                       dev_t     st_dev;      /* device of inode */
                       ino_t     st_ino;      /* inode number */
                       short     st_mode;     /* mode bits */
                       short     st_nlink;    /* number of links to file */
                       short     st_uid;      /* owners user id */
                       short     st_gid;      /* owners group id */
                       dev_t     st_rdev;     /* for special files */
                       off_t     st_size;     /* file size in characters */
                       time_t    st_atime;    /* time last accessed */
                       time_t    st_mtime;    /* time last modified */
                       time_t    st_ctime;    /* time originally created */
                   };

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by steveski74
                  Why not stick to the old faithful ? :
                  It's not portable. Only works for Unix.

                  Comment

                  • lini
                    New Member
                    • Mar 2007
                    • 12

                    #10
                    cross-platform:

                    Code:
                      ifstream is;
                      is.open ("test.txt", ios::binary );
                    
                      // get length of file:
                      is.seekg (0, ios::end);
                    cheers,
                    lini

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      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.

                      Comment

                      Working...