GONGJUNRUI123
2018-01-29 23:32
采纳率: 50%
浏览 2.1k
已结题

用stat函数无法获取文件大小

各位,我打算用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;
    }
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • yangjiexia 2018-01-30 01:25

    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;
    }
    

    }

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题