Finally wrote the function I wanted to make with help of pass-by-reference.
The code below would allow to load a ASCII file content (hopefully with binary file too) into an uninitialized string.
PHP Code:
[code]
#include <stdio.h>
#include <stdlib.h>
int flen(FILE *flen_file); //returns length of a file
int fget(char **dest, FILE *fget_file); //returns all the content of a file
int fload(char **dest, const char *file_name); //loads file_name into dest
int main(void) {
char *content;
int err = fload(&content, "test.txt");
if (err == 0) {
printf("%s\n", content);
}
free(content);
return 0;
}
int flen(FILE *flen_file) {
int size;
fseek(flen_file, 0, SEEK_END);
size = ftell(flen_file);
fseek(flen_file, 0, SEEK_SET);
return size;
}
int fget(char **dest, FILE *fget_file) {
int LOF = flen(fget_file);
*dest = malloc(sizeof(char) * (LOF + 1));
char *d = *dest;
int i = 0;
while (i < LOF) {
*d++ = fgetc(fget_file);
i++;
}
*d = '\0';
return 0;
}
int fload(char **dest, const char *file_name) {
FILE *tmp_file = fopen(file_name, "rb");
if (tmp_file != NULL) {
if (fget(dest, tmp_file) == 0) {
return 0;
} else {
return 2;
}
} else {
return 1;
}
}
[/code]
Now tell me those where created from random commands.