c get file size
To get the size of a file in C, you can use the fseek
and ftell
functions. Here's an example of how you can do it:
#include <stdio.h>
int main() {
FILE *file;
long size;
// Open the file
file = fopen("filename.txt", "rb");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
// Seek to the end of the file
fseek(file, 0, SEEK_END);
// Get the position of the file pointer
size = ftell(file);
printf("File size: %ld bytes\n", size);
// Close the file
fclose(file);
return 0;
}
This code opens a file called "filename.txt" in binary mode. It then uses fseek
to move the file pointer to the end of the file, and ftell
to get the position of the file pointer, which corresponds to the size of the file. Finally, it prints the size in bytes and closes the file.
Note: Make sure to replace "filename.txt" with the actual name of the file you want to get the size of.