Don't Reinvent The Wheel
#Don't Reinvent The Wheel
ItIt might be better to use built-in functions that already do what you want. You can use stat()
to get the size of a file. You don't even need read or write permission to the file. You need to #include <sys/stat.h>
and pass it a path to a file and a pointer to a struct stat
. (On Windows you can use _stat()
et al.) It would look something like this:
#include <sys/stat.h>
long get_file_size(const char *file_path) {
/* Debug: checking the input arguments. */
assert(file_path != NULL);
struct stat status;
int err = stat(file_path, &status);
long size = -1;
if (err == -1)
{
LOG("Error: can't get the of the file. The "
"reason: \"%s\".\n", strerror(errno));
}
else
{
size = status.st_size;
}
return size;
}
While this function is about as long as yours, it doesn't need to open the file and seek to the end. You can use it to get sizes on files you can't read, for example. This is handy in situations where you want to let a user check how much disk space is used, for example. You don't want to open
, seek
, close
each file.
Remove Unnecessary Stuff
#Remove Unnecessary Stuff
II don't see a lot of point in your open_file()
function. It does one more thing than fopen()
alone, which is print out an error if it fails. That's usually something you want to leave up to the caller, anyway, as one message is probably not appropriate for all the cases where you open a file.
Watch Out For Errors!
#Watch Out For Errors!
YourYour get_file_contents()
function has an error in it. In the case where the memory can't be allocated, it reads data into contents
, which will be NULL
which will crash.
#Don't Reinvent The Wheel
It might be better to use built-in functions that already do what you want. You can use stat()
to get the size of a file. You don't even need read or write permission to the file. You need to #include <sys/stat.h>
and pass it a path to a file and a pointer to a struct stat
. (On Windows you can use _stat()
et al.) It would look something like this:
#include <sys/stat.h>
long get_file_size(const char *file_path) {
/* Debug: checking the input arguments. */
assert(file_path != NULL);
struct stat status;
int err = stat(file_path, &status);
long size = -1;
if (err == -1)
{
LOG("Error: can't get the of the file. The "
"reason: \"%s\".\n", strerror(errno));
}
else
{
size = status.st_size;
}
return size;
}
While this function is about as long as yours, it doesn't need to open the file and seek to the end. You can use it to get sizes on files you can't read, for example. This is handy in situations where you want to let a user check how much disk space is used, for example. You don't want to open
, seek
, close
each file.
#Remove Unnecessary Stuff
I don't see a lot of point in your open_file()
function. It does one more thing than fopen()
alone, which is print out an error if it fails. That's usually something you want to leave up to the caller, anyway, as one message is probably not appropriate for all the cases where you open a file.
#Watch Out For Errors!
Your get_file_contents()
function has an error in it. In the case where the memory can't be allocated, it reads data into contents
, which will be NULL
which will crash.
Don't Reinvent The Wheel
It might be better to use built-in functions that already do what you want. You can use stat()
to get the size of a file. You don't even need read or write permission to the file. You need to #include <sys/stat.h>
and pass it a path to a file and a pointer to a struct stat
. (On Windows you can use _stat()
et al.) It would look something like this:
#include <sys/stat.h>
long get_file_size(const char *file_path) {
/* Debug: checking the input arguments. */
assert(file_path != NULL);
struct stat status;
int err = stat(file_path, &status);
long size = -1;
if (err == -1)
{
LOG("Error: can't get the of the file. The "
"reason: \"%s\".\n", strerror(errno));
}
else
{
size = status.st_size;
}
return size;
}
While this function is about as long as yours, it doesn't need to open the file and seek to the end. You can use it to get sizes on files you can't read, for example. This is handy in situations where you want to let a user check how much disk space is used, for example. You don't want to open
, seek
, close
each file.
Remove Unnecessary Stuff
I don't see a lot of point in your open_file()
function. It does one more thing than fopen()
alone, which is print out an error if it fails. That's usually something you want to leave up to the caller, anyway, as one message is probably not appropriate for all the cases where you open a file.
Watch Out For Errors!
Your get_file_contents()
function has an error in it. In the case where the memory can't be allocated, it reads data into contents
, which will be NULL
which will crash.
#Don't Reinvent The Wheel
It might be better to use built-in functions that already do what you want. You can use stat()
to get the size of a file. You don't even need read or write permission to the file. You need to #include <sys/stat.h>
and pass it a path to a file and a pointer to a struct stat
. (On Windows you can use _stat()
et al.) It would look something like this:
#include <sys/stat.h>
long get_file_size(const char *file_path) {
/* Debug: checking the input arguments. */
assert(file_path != NULL);
struct stat status;
int err = stat(file_path, &status);
long size = -1;
if (err == -1)
{
LOG("Error: can't get the of the file. The "
"reason: \"%s\".\n", strerror(errno));
}
else
{
size = status.st_size;
}
return size;
}
While this function is about as long as yours, it doesn't need to open the file and seek to the end. You can use it to get sizes on files you can't read, for example. This is handy in situations where you want to let a user check how much disk space is used, for example. You don't want to open
, seek
, close
each file.
#Remove Unnecessary Stuff
I don't see a lot of point in your open_file()
function. It does one more thing than fopen()
alone, which is print out an error if it fails. That's usually something you want to leave up to the caller, anyway, as one message is probably not appropriate for all the cases where you open a file.
#Watch Out For Errors!
Your get_file_contents()
function has an error in it. In the case where the memory can't be allocated, it reads data into contents
, which will be NULL
which will crash.