I created below code in a Objective C program to get the size of a file in a directory and I must do it in C without using any Objective-C methods. This works fine and gives the correct size when checking it with small size files. But when it comes to large files (tested with 7GB file) this size variable returns the size of the file as 0. Can anyone help me to solve this issue?
//filePath is a string parameter which takes the path of the file
FILE *fp=nil;
const char *filePathInToChar = [filePath UTF8String];
fp = fopen(filePathInToChar, "rb");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
Your problem is that
ftellreturns the current position in the filestream aslong intwhich on a 32bit system has usually a maximum value of 2^31 – or 2GB in other words.You can use
stat(so you don’t actually need to open and close the file):off_tshould belong longwhich is usually 64bit.