C. How to get the file length and C basic file length?
Introduction
One day I saw that the returned file length code returned values are long, which is strange. Generally, the maximum length of 32-bit long is 2 GB.
There are too many large files. It is estimated that a Dota2 installation package requires more than 10 Gb. Generally, C gets the file length code.
/** Get the FILE length, a common old syntax * path: FILE path *: returned FILE length */long file_getsize (const char * path) {FILE * txt; long rt; if ((! Path) |! (Txt = fopen (path, "rb") return 0; fseek (txt, 0, SEEK_END); rt = ftell (txt); fclose (txt ); return rt ;}
The above routine is more straightforward. Write a test code
# Include <stdio. h>/** get the file length. A common formula is * path: file path *: Return file length */long file_getsize (const char * path ); int main (int argc, char * argv []) {const char * path; int I = 1; while (I <argc) {path = argv [I]; printf ("% s => % ld \ n", path, file_getsize (path); ++ I;} return 0 ;}
Check the normal test result.
For fopen "rb", B indicates that the binary stream is used for processing. The t text mode is used by default. The former is faster, and the latter performs some special processing.
The main reason is that different systems are born to process different businesses for line breaks. We recommend that you use the B binary processing method faster. (The 21st century was the age of B, all of which were BBB)
Preface
Next, let's make another experiment and look at the following large file. Continue to test it with the above Code. first look at the test file.
The test results are as follows:
At this time, we need to use the new file operation code to find a solution. In fact, the above ftell method gets the code, and the performance of moving the file pointer back and forth is very low.
The operating system knows the file size and directly asks if it is the fastest. First, write a piece of code on the window.
# Include <stdio. h> # include <sys/types. h> # include <sys/stat. h> # include <inttypes. h>/** get the file length. For windows, use * path: file path *: Return file length */int64_t file_getsize (const char * path); int main (int argc, char * argv []) {const char * path; int I = 1; while (I <argc) {path = argv [I]; printf ("% s => % ld \ n", path, file_getsize (path); ++ I ;}return 0 ;}/ ** get the file length, windows: * path: file path *: returned file length */int64 _ Tfile_getsize (const char * path) {struct _ stat64 info = {0}; if (! Path |! * Path) return 0; _ stat64 (path, & info); return info. st_size ;}
The above is the complete test code. It mainly uses the _ stat64 function under sys/stat. h to get the 8-byte file size representation.
We will continue the test. The results are satisfactory.
Here we have finished the test on the window.
Body
Let's test it on linux. first look at the code I wrote main_linux.c.
# Include <stdio. h> # include <sys/types. h> # include <sys/stat. h> # include <inttypes. h>/** get the file length. linuxs platform usage * path: file path *: returned file length */int64_t file_getsize (const char * path); int main (int argc, char * argv []) {const char * path; int I = 1; // test demo struct stat info; printf ("info. st_size size = % ld \ n ", sizeof (info. st_size); printf ("unsigned long size = % ld \ n", sizeof (unsigned long); while (I <ar Gc) {path = argv [I]; printf ("% s => % ld \ n", path, file_getsize (path); ++ I;} return 0 ;} /** get the file length. For linux, use * path: file path *: returned file length */int64_tfile_getsize (const char * path) {struct stat info = {0 }; if (! Path |! * Path) return 0; stat (path, & info); return info. st_size ;}
Compile command
gcc -Wall -ggdb2 -o main_linux.out main_linux.c
Check the sys/stat. c source code later.
If you are interested, you can see that linux will automatically help us determine whether it is stat64 OR stat based on the platform. I use 64-bit, and the default stat is also stat64.
The returned long value is 8 bytes.
This also solves a problem. Generally, the off_t structure is an 8-byte unsigned long structure. The window structure is a long structure. Different platform implementations are different.
If you want this function to be cross-platform, you need to do it on the window.
#if defined(_MSC_VER)
# define stat _stat64
#endif
Is it easy. we can get the file length code across platforms. ah, this language has too many details, which is not conducive to production. masters are also old, and there are fewer opportunities for improvement, which is too stable.
If it is only for fast performance, then no one is more suitable than C, and more fast and lightweight. Everything is a choice, no best, only the most suitable.
Postscript
Errors are inevitable. Welcome to improve communication. Maybe when we were young, we fantasized about becoming a hero, and eventually became a house slaves with hard-earned money.
People with dreams are enviable,It is a pity to have a dream.Love me and be happy ~~~~~~~~~