各位,我打算用stat里面的st-size 函数获取文件的大小,但是怎么调试得到的值就是0.
int
main (void)
{
char *fileName = "abcd.txt";
SM_FileHandle fh;
createPageFile(fileName);
openPageFile (fileName, &fh);
int a = fh.totalNumPages;
printf("%d+%s", a, "main");
return 0;
}
RC createPageFile (char *fileName){
FILE *pageFile;
pageFile = fopen(fileName,"w"); //Creates an empty file for both reading and writing.
// for(int i = 0; i < PAGE_SIZE; i++){ //Creates one page in the file. The size of page is 4096 bytes
// fwrite("\0", 1, 1, pageFile);
// }
fwrite("abc", 3, 1, pageFile);
struct stat fileStat;
stat(fileName, &fileStat);
int fileSize = fileStat.st_size; //Get the size of file.
printf("%d+%s\n", fileSize, "create file");
return RC_OK;
}
RC openPageFile (char *fileName, SM_FileHandle *fHandle){
FILE *pageFile;
pageFile = fopen(fileName,"r"); //Opens the file.
if(pageFile == NULL){ //If cannot found the file, return "RC_FILE_NOT_FOUND".
return RC_FILE_NOT_FOUND;
}else{
struct stat fileStat;
stat(fileName, &fileStat);
int fileSize = fileStat.st_size; //Get the size of file.
fHandle->fileName = fileName;
fHandle->totalNumPages = fileSize/PAGE_SIZE;
fHandle->curPagePos = 0;
fHandle->mgmtInfo = pageFile;
printf("%d+%s\n", fileSize, "open file");
return RC_OK;
}
}