首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

VC里有没有检查文件是否为空的函数,该怎么处理

2012-03-18 
VC里有没有检查文件是否为空的函数用ifstream读取了一个文件怎么判定这个文件是否为空[解决办法]记录文件

VC里有没有检查文件是否为空的函数
用ifstream读取了一个文件
怎么判定这个文件是否为空

[解决办法]
记录文件开头文件指针偏移值,把文件指针调到文件末尾,然后用当前偏移值减文件头的偏移值,看是不是为0
[解决办法]
可以打开文件读取一个字节
返回-1就是空
[解决办法]
肯定有办法的
是不是有现成的函数来判断我不清楚了
ls两的方法lz貌似都可以
[解决办法]
用系统API:GetFileSize
[解决办法]

探讨

用系统API:GetFileSize

[解决办法]
_filelength, _filelengthi64
Get the length of a file.

long _filelength( int handle );

__int64 _filelengthi64( int handle );

Function Required Header Compatibility 
_filelength <io.h> Win 95, Win NT 
_filelengthi64 <io.h> Win 95, Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

Both _filelength and _filelengthi64 return the file length, in bytes, of the target file associated with handle. Both functions return a value of –1L to indicate an error, and an invalid handle sets errno to EBADF.

Parameter

handle

Target file handle

Example

/* CHSIZE.C: This program uses _filelength to report the size
 * of a file before and after modifying it with _chsize.
 */

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

void main( void )
{
int fh, result;
unsigned int nbytes = BUFSIZ;

/* Open a file */
if( (fh = _open( "data", _O_RDWR | _O_CREAT, _S_IREAD 
| _S_IWRITE )) != -1 )
{
printf( "File length before: %ld\n", _filelength( fh ) );
if( ( result = _chsize( fh, 329678 ) ) == 0 )
printf( "Size successfully changed\n" );
else
printf( "Problem in changing the size\n" );
printf( "File length after: %ld\n", _filelength( fh ) );
_close( fh );
}
}


Output

File length before: 0
Size successfully changed
File length after: 329678


File Handling Routines

See Also _chsize, _fileno, _fstat, _fstati64, _stat, _stati64

热点排行