How does a program determine the size of a file without reading it in its entirety?

In question Using C ++ filestreams (fstream), how can you determine the file size? , the best answer is the following C ++ snippet:

ifstream file("example.txt", ios::binary | ios::ate);
return file.tellg();

By running it myself, I noticed that the size of arbitrarily large files can be determined instantly and with a single read operation.

I usually assumed that to determine the size of the file, you would need to move it byte by byte, adding it to the byte counter. How is this achieved instead? Metadata?

+3
source share
3 answers

File size is embedded in file metadata in the file system. Different file systems have different ways of storing this information.

. , . - , , ex3 ntfs, , .

+2

. GI Joe , stat posix:

stat (3) manpage

struct stat     statbuf;
stat("filename.txt", &statbuf);
printf("The file is %d bytes long\n", statbuf.st_size);
0

ios::ate , , .

tellg . ios::binary.

, , , , ( ). , , - , .

, , fooobar.com/questions/368313/....

-1

All Articles