文件大小未在c中显示

时间:2012-11-07 07:15:52

标签: c

我试图在c中获取文件的大小,但我的代码没有显示任何文件大小的输出。这是我的代码

#include<stdio.h>
#include<fcntl.h>
#include<io.h>
#include<BIOS.H>
#include<DOS.H>

unsigned int handle;
void main()
{
    union REGS regs;
    unsigned long int size;
    handle = open("c:\\abc.txt",O_RDONLY);
    regs.x.bx = handle;
    regs.h.ah = 0x42;
    regs.h.al = 0x02;    //correction
    regs.x.cx = 0;
    regs.x.dx = 0;
    int86(0x21,&regs,&regs);
    *((int*)(&size)) = regs.x.ax;
    *(((int*)(&size))+1) =regs.x.dx;
    printf ("Size is %d" ,size);
}

有人可以告诉我为什么输出没有显示

1 个答案:

答案 0 :(得分:1)

使用fstat

#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main( void )
{
   struct _stat buf;
   int fh, result;

   fh = _open( "c:\\abc.txt", O_RDONLY );

   /* Get data associated with "fh": */
   result = _fstat( fh, &buf );

   /* Check if statistics are valid: */
   if( result != 0 ) {
      printf( "Bad file handle\n" );
   } else {
      printf( "File size     : %ld\n", buf.st_size );
   }
   _close( fh );
}