C Function

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 )

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