Protected: php & C 計算檔案大小差別

input
<?php
$filename = $argv[1];
echo $filename . ": " . filesize($filename) . " bytes\r\n";
?>


output
check_mail.html: 186 bytes

input

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main ( int argc, char *argv [] ){
int status;

if ( 2 != argc ){
fprintf ( stderr, "Usage: %s <filename>\n", argv [ 0 ] );
status = EXIT_FAILURE;
}else{
FILE *fp;
fp = fopen ( argv [ 1 ], "rb" );
if ( !fp ){
fprintf ( stderr, "Cannot open file \"%s\" for reading.[Error %d]\n", argv [ 1 ], errno );
status = EXIT_FAILURE;
}else{
char c;
long unsigned size = 0;

while ( fread ( &c, sizeof c, 1, fp ) ){
size++;
}

fclose ( fp );
printf ( "Size of file (in bytes): %lu\n", size );
#if 0
if ( size > 1 << 10 ){
/*** 1 KB ***/
printf ( "[%lu KB]\n", size / ( 1 << 10 ) );
}
#endif
status = EXIT_SUCCESS;
}
}

#if 0
printf ( "Returning status code: %d\n", status );
#endif
return status;
}

output
Size of file (in bytes): 186

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s