March 19th, 2004, 08:03 PM
Size of File in C
I recently created a program that replaces the contents of a file with different contents. One of my requirements was that the file was same size when I was done. Coding in C the file size is handled by the system. On google I found links to truncate a file but nothing telling me how to get the size.
I tried size = sizeof (filepointer); where filepoint is of type FILE* and the file has been opened but that returns 8. I tried sizeof (pathtofile) but that returns the size of the path. What I ended up doing was the following:
int FileSize(FILE *index)
int size = 0;
I was wondering if there is a more eloquent way to achieve the same result. Any suggestions would be greatly appreciated. It should return size - 1 (I handle that later in the program).
program built using vc++ from visual studio 6
If you spend more on coffee than on IT security, you will be hacked. What\'s more, you deserve to be hacked.
-- former White House cybersecurity adviser Richard Clarke
March 20th, 2004, 03:30 PM
Hi. If you can use C++, read my post in this thread:
If you have to use C, replace seekg() with fseek() and tellg() with ftell() in the example in my post.
Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds