Using sizeof()

Hi all,

I have a question regarding usage of sizeof(). I have a binary file consisting of an unknown number of "tasks"(task is a struct that is defined by me). Now I want to find out how many "tasks" there are in this binary file. So the question is, can I use sizeof() to determine the size of the whole binary file? My code currently is as follows:

int taskCount(char binaryFileName[],fstream& file, task a)
{
file.open(binaryFileName,ios::in | ios::binary);

int count = sizeof(file) / sizeof(a);

file.close();

return count;
}

If not, any suggestions on what could work? Thanks for the help.
closed account (o1vk4iN6)
sizeof() can only be used to determine the size of variables that is know by the compiler during compile, so what you are getting is the size in memory of the variable file. You can use seekg() and tellg() to get the size of the file, much like in this example:

http://www.cplusplus.com/reference/iostream/istream/seekg/
Got it, thanks!
Topic archived. No new replies allowed.