belajar programming

file_put_contents()

Posted by sphinx on February 13, 2008.

/* file_put_contents() function in C - PHP Equivalent by sphinx */
int file_put_contents(char *file, char *data) { long long int size; FILE *fp; // later we could improve to have mode 0 or 1, //  as 1 == append if exist, and 0 == rewrite int mode = 0; size = strlen(data); if(mode == 0) { fp = fopen(file,”wb”); if(fp) { fwrite(data, 1, size, fp); } else return 0; } fclose(fp); return 1; }
Read Full Post | Make a Comment ( None so far )

file_get_contents()

Posted by sphinx on February 12, 2008.

/* file_get_contents() function in C - PHP Equivalent by sphinx */

// need filesize() function

char *file_get_contents(char *file) { char *tmp; long long int size; FILE *fp; size = filesize(file); tmp = malloc(size + 1); fp = fopen(file, “rb”); if(fp) { fread(tmp, 1, size, fp); } else { return 0; } fclose(fp); return (char *)tmp; }

Read Full Post | Make a Comment ( None so far )

perbandingan waktu execution dengan php dan C

Posted by sphinx on February 8, 2008.

Iseng2 mau bandingin execution time dr file i/o menggunakan php dan C, sangat terlihat perdaan yang jauh sekali. Menggunakan C execution timenya 0.001s dan menggunakan PHP execution timenya 0.047s. (s = second = detik)

Dengan C:

[firman@muse ch]$ time ./test
#!/bin/ch
#include <stdio.h>
int main(void) {
printf("hello world\n");
return 0;
}
real    0m0.001s
user    0m0.000s
sys     0m0.001s

Source:

int main(void) {
printf(”%s\n”, file_get_contents(”a”) );
return 0;
}

Perhatian! file_get_contents() bukan fungsi standard ANSI C!

Dengan PHP:

[firman@muse ch]$ time php test.php
#!/bin/ch
#include <stdio.h>
int main(void) {
printf("hello world\n");
return 0;
}
real    0m0.047s
user    0m0.031s
sys     0m0.016s

Source:

<?php echo file_get_contents(”a”).”\n”; ?>

Read Full Post | Make a Comment ( None so far )

filesize()

Posted by sphinx on February 8, 2008.

/* filesize() function in C - PHP Equivalent by sphinx */
long long int filesize(char *file) {
  FILE *fp;
  long long int i = 0;
  fp = fopen(file,"rb");
  while(fgetc(fp) != EOF) {
  ++i; }
  fclose(fp);
  return i;
}

example usage:

int main(void) {
  printf("%ld", filesize("/path/to/filename"));
  return 0;
}
Read Full Post | Make a Comment ( 1 so far )

Liked it here?
Why not try sites on the blogroll...

Get a Free Blog @ plinplan.net