How to find size of a file in bytes??

How would I find the size of a filename in bytes?? For example, I opened a file using arguements passed into main, but how would I find the filename's size?? I need to know because I want to implement a progress meter in one of my C programs. Thanks a lot


Comments

  • You can use the following code to do this (excuse my inability to use tabbing on this message board, you can hit reply to see it pseudo-tabbed in the reply text box):

    unsigned int fgetsize(char *path) {
    unsigned int fsize;
    FILE *fp = fopen(path,"rt");
    if(fp!=NULL) {
    fseek(fp,0,SEEK_END);
    fsize = ftell(fp);
    return fsize;
    } else {
    return 0;
    }
    }


  • You can use the following code to do this (excuse my inability to use tabbing on this message board, you can hit reply to see it pseudo-tabbed in the reply text box):

    unsigned int fgetsize(char *path) {
    unsigned int fsize;
    FILE *fp = fopen(path,"rt");
    if(fp!=NULL) {
    fseek(fp,0,SEEK_END);
    fsize = ftell(fp);
    fclose(fp);
    return fsize;
    } else {
    return 0;
    }
    }


  • : How would I find the size of a filename in bytes?? For example, I opened a file using arguements passed into main, but how would I find the filename's size?? I need to know because I want to implement a progress meter in one of my C programs. Thanks a lot
    :

    Use the stat function, here is an example:

    #include
    #include

    void main(){
    struct _stat lBuf;

    if( _stat( "...filename...", &lBuf ) )
    perror( "_stat" );
    else
    printf( "File size: %ld
    ", lBuf.st_size );

    }


    Eyal Lupu




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories