In order to read a whole file as a string using standard libraries you have to allocate memory, terminate the string, determine size of the file and size of each element.
This could happen with fread
until I decided to automatize it using this:
char *readFile (FILE* fp, char *source)
{
unsigned long long int size;
unsigned long long int i = 0;
int chr;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
source = calloc(size + 1, sizeof(char));
fseek(fp, 0, SEEK_SET); // rewind
while(chr != EOF)
source[i++] = (chr = fgetc(fp));
source[size] = '0円'; // Terminate string
return(source);
}
1 Answer 1
You let the user pass in a char*
but you don't use it. you might as well remove it and declare it as a local variable.
char *readFile (FILE* fp)
{
char* source;
Some FILE*
won't let you seek to the end (stdin and socket streams for example). You should check the return value of fseek
and fall back to the classic fread
then realloc
, rinse and repeat method on error.
A single fread
is more efficient than the repeated fgetc
; so you can replace the while with:
int readBytes = fread(source, size, 1, fp);
string[readBytes] = '0円';
-
\$\begingroup\$ How come I don't use it? The string will be copied to the
char*
destination the user specified. \$\endgroup\$Genis– Genis2015年01月30日 09:36:34 +00:00Commented Jan 30, 2015 at 9:36 -
\$\begingroup\$ The first thing you do with source is assign a freshly
calloc
ated chunk of memory to it. This overwrites the buffer the caller passed in and means that in all casesbuffer!=readFile(fp, buffer);
\$\endgroup\$ratchet freak– ratchet freak2015年01月30日 09:39:56 +00:00Commented Jan 30, 2015 at 9:39 -
\$\begingroup\$ I don't understand. \$\endgroup\$Genis– Genis2015年01月30日 09:42:34 +00:00Commented Jan 30, 2015 at 9:42
-
\$\begingroup\$ @Genis For all intents and purposes you ignore the value the caller passed in source. \$\endgroup\$ratchet freak– ratchet freak2015年01月30日 09:46:28 +00:00Commented Jan 30, 2015 at 9:46