PDA

Click to See Complete Forum and Search --> : Size of File in C


dspeidel
March 19th, 2004, 08:03 PM
Hi All,

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;
while (!feof(index)){
fgetc(index);
size ++;
}//close while
return size;

}//close FileSize

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).

Thanks,
-D

program built using vc++ from visual studio 6

jdenny
March 20th, 2004, 03:30 PM
Hi. If you can use C++, read my post in this thread:
http://www.antionline.com/showthread.php?s=&threadid=255646

If you have to use C, replace seekg() with fseek() and tellg() with ftell() in the example in my post.

Peace always,
<jdenny>