c语言如何判断为空可用读文件操作函数的返回值来判定,如:
函数: int fgetc(FILE *stream); //从参数stream 所指的文件中读取一个字符. 若读到文件尾而无数据时便返回EOF.
当以读方式打开文件后,第一次使用fgetc()就返回EOF,则表明文件是空文件。
参考代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <stdio.h>
void
main()
{
FILE
*fp;
int
ch;
fp=
fopen
(
"新建文本文档.txt"
,
"r"
);
ch=
fgetc
(fp);
if
( ch==EOF )
printf
(
"file is null\n"
);
else
printf
(
"read a char : %c\n"
);
fclose
(fp);
}
|
注:
EOF不是文件中的一个字符,而是一个标志,C中定义EOF为-1,可在stdio.h中查到。
读文件函数,如:fgetc()在读到文件尾而无数据时便会返回EOF。
空文档是指字节数是0的文档。如果文档中有字符,则不是空文档。
https://zhidao.baidu.com/question/744909786251886972.html{
if(!pszPath || !strcmp("", pszPath))
{ return UNFIND; }
string strPath = pszPath;
strPath += "/*";
_finddata_t findD_F = {0};
long hMove = _findfirst(strPath.c_str(), &findD_F);
if(hMove > 0)
{
int iCount = 0;
do{
if(strcmp(".", findD_F.name) && strcmp("..", findD_F.name))
{
++iCount;
break;
}
}while(!_findnext(hMove, &findD_F));
_findclose(hMove);
if(iCount > 0)
{ return NOTEMPTY; }
return ISEMPTY;//为空
}
return UNFIND;
}
实例:
#include <stdio.h>
#include <io.h>
int main(void)
{
if ( !access("C://windows",0) )
puts("C://windows EXISITS!");
else
puts("C://windows DOESN'T EXISIT!");
return 0;
}
方法一:access函数判断文件夹或者文件是否存在
函数原型: int access(const char *filename, int mode);
所属头文件:io.h
filename:可以填写文件夹路径或者文件路径
mode:0 (F_OK) 只判断是否存在
2 (R_OK) 判断写入权限
4 (W_OK) 判断读取权限
6 (X_OK) 判断执行权限
用于判断文件夹是否存在的时候,mode取0,判断文件是否存在的时候,mode可以取0、2、4、6。 若存在或者具有权限,返回值为0;不存在或者无权限,返回值为-1。
错误代码
EACCESS 参数pathname 所指定的文件不符合所要求测试的权限。
EROFS 欲测试写入权限的文件存在于只读文件系统内。
EFAULT 参数pathname指针超出可存取内存空间。
EINVAL 参数mode 不正确。
ENAMETOOLONG 参数pathname太长。
ENOTDIR 参数pathname为一目录。
ENOMEM 核心内存不足
ELOOP 参数pathname有过多符号连接问题。
EIO I/O 存取错误。
特别提醒:使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题。
实例:
#include <stdio.h>
#include <io.h>
int main(void)
{
if ( !access("C://windows",0) )
puts("C://windows EXISITS!");
else
puts("C://windows DOESN'T EXISIT!");
return 0;
}
方法二:fopen函数判断文件是否存在
函数原型:FILE *fopen (char *filename, char *type);
filename:文件路径
type:打开文件的方式(有r、w、r+、w+、a、rb、wb等等)
用于判断文件是否存在可以使用 r 或者 rb ,因为使用 其它方式的话,可能会自动建立文件。 返回值为NULL(打不开)和正数(能打开)。
特别提醒:用这种方法做出的判断是不完全正确的,因为有的文件存在,但是可能不可读。
3、http://www.cnblogs.com/project/archive/2010/12/02/1894494.html
在我们平时的编程时,经常需要判断文件或者目录是否存在,相对来说判断文件的存在性比较简单,目录则比较复杂。
下面就详细的介绍几种方法。
首先关于判断文件的存在性:
一、ifstream
在C++中,可以利用ifstream文件输入流,当我们直接使用ifstream来创建文件输入流的时候,如果文件不存在则流创建失败。
if ( ! fin)
{
std::cout << " can not open this file " << endl;
这是c++中最常用的方式。
二、File
C中也是同样道理,我们可是File的相关操作。
if (fh == NULL)
{
printf( " %s " , " can not open the file " );
}
三、_access
当然C中还有一种方式是直接调用c的函数库。
就是函数 int _access(const char* path,int mode);
这个函数的功能十分强大。
可以看看msdn的详细介绍
#include < stdio.h >
#include < stdlib.h >
int main( void )
{
// Check for existence.
if ( (_access( " crt_ACCESS.C " , 0 )) != - 1 )
{
printf_s( " File crt_ACCESS.C exists.\n " );
// Check for write permission.
// Assume file is read-only.
if ( (_access( " crt_ACCESS.C " , 2 )) == - 1 )
printf_s( " File crt_ACCESS.C does not have write permission.\n " );
}
}
这三种方式算是判断文件存在比较简单快捷的方法了。
现在来说说判断目录存在的一些方法。
一、FindFirstFile
在C++中可以调用系统的一些函数,但这种方法稍微显得复杂一些。
bool rValue = false ;
HANDLE hFind = FindFirstFile(strPath.c_str(), & wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
std::cout << " this file exists " << endl;
}
FindClose(hFind);
二、_stat()
现在介绍一个轻量级的方法
在windows中可以使用_stat() 函数。
声明:int _stat(const char* path, struct _stat* buffer);
这个函数使用起来非常方便,如下:
if ((_stat(dir.c_str(), & fileStat) == 0 ) && (fileStat.st_mode & _S_IFDIR))
{
isExist = true ;
}
_S_IFDIR 是一个标志位,如果是目录的话,该位就会被系统设置。
在linux底下也有相对应的函数stat();
使用方法基本相同:
if ((stat(dir.c_str(), & fileStat) == 0 ) && S_ISDIR(fileStat.st_mode))
{
isExist = true ;
}
唯一不同的地方我使用了一个macro, S_ISDIR来判断文件是否存在,原理实际都一样的。
上面就是自己使用过的几种判断文件和目录存在性的一些经验了,希望对大家有所帮助。
4、http://bbs.youkuaiyun.com/topics/270008419 http://bbs.youkuaiyun.com/topics/90255402