c判断文件是文件夹还是文件夹
㈠ C 判断文件或文件夹是否存在
C/C++中判断某一文件或目录是否存在
1.C++很简单的一种办法:#include<iostream#include<fstreamusingnamespacestd;#defineFILENAME"stat.dat"intmain(){fstream_file;
_file.open(FILENAME,ios::in);if(!_file){cout<<FILENAME<<"没有被创建";}else{cout<<FILENAME<<"已经存在";}return0;}
2.利用 c 语言的库的办法:
函数名: access功能: 确定文件的访问权限用法: int access(const char *filename, intamode);
以前一直没用过这个函数,今天调试程序发现了这个函数,感觉挺好用,尤其是判断一个文件或文件夹是否存在的时候,用不着再find了,文件的话还可以检测读写权限,文件夹的话则只能判断是否存在,下面摘自MSDN:int_access(constchar*path,
intmode);Return Value
Each of these functions returns 0 if the file has the given
mode. The function returns –1 if the named file does not exist or
is not accessible in the given mode; in this case,errnois set as follows:EACCESAccess denied: file’s permission setting does not
allow specified access.
ENOENTFilename or path not found.
ParameterspathFile or directory pathmodePermission settingRemarksWhen used with files, the_accessfunctiondetermines whether the specified file exists and can be accessed as
specified by the value ofmode
. When used with
directories,
_accessdetermines only whether the
specified directory exists; in Windows NT, all directories have
read and write access.
modeValue
Checks File For00
Existence only02
Write permission04
Read permission06
Read and write permissionExample#include<io.h#include<stdio.h#include<stdlib.hvoidmain(void){if((_access("ACCESS.C",0))!=-1){printf("FileACCESS.C
#include
<stdio.h>
#include
<sys/stat.h>
#include
<unistd.h>
int
main(int
argc,char
*argv[])
{
struct
stat
st;
printf("%s",argv[1]);
stat(argv[1],&st);
if
(S_ISDIR(st.st_mode))
printf("is
a
dir\n");
else
printf("is
not
a
dir\n");
return
0;
}
虚拟机上测过了.
是验证输入的第一个参数是不是目录.
㈢ c#判断路径 是文件还是文件夹
如果文件名同目录名完全一样,你没法判断到底是个文件还是目录,因为物理磁盘上可能两者都有(完全同名)。
比如说c:\windows,既可能是windows目录,也可能是个主名为"windows"且没有扩展名的文件,所以但判断路径字符串本身是无法判断的。
㈣ c++ 判断一个路径是文件夹还是文件
WIN32_FIND_DATAAFindFileData;
FindFirstFileA("c:\1.txt",&FindFileData);
if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
//是文件夹
}
else
{
//是文件
}
可能需要#include<windows.h>
㈤ C语言判断一个字符串是文件还是文件夹
一般来说在C语言中读取txt文件的信息有两种方法,一种是使用C语言标准文件I/O中的fopen()、fread()等等函数,一种是调用操作系统中的API函数,比如Windows上的ReadFile()、OpenFile()等等,现在操作系统一般都具备内存文件映射功能,对于大的txt文件,一般都使用这种方式操作。下面是一个使用C语言标准文件I/O操作文件的例子。
#include<stdio.h>
FILE*stream;
void main(void)
{
long l;
float fp;
char s[81];
char c;
stream=fopen("fscanf.out","w+");
if(stream==NULL)
printf("Thefilefscanf.outwasnotopened\n");
else
{
fprintf(stream,"%s%ld%f%c","hello world",
65000,3.14159,'x');
/*Setpointertobeginningoffile:*/
fseek(stream,0L,SEEK_SET);
/*Readdatabackfromfile:*/
fscanf(stream,"%s",s);
fscanf(stream,"%ld",&l);
fscanf(stream,"%f",&fp);
fscanf(stream,"%c",&c);
/*Outputdataread:*/
printf("%s\n",s);
printf("%ld\n",l);
printf("%f\n",fp);
printf("%c\n",c);
fclose(stream);
}
}
㈥ 如何用C语言判断文件夹内是否有文件夹或文件
举例来说:FILE*fp=fopen("dict.txt","r");charbuf[1024];if(fp!=(FILE*)NULL){while(fgets(buf,sizeof(buf),fp))//从文件中读入一行字符串,保存在buf中,直到读完所有字符串{//处理读入的字符串buf}fclose(fp);}
㈦ C#,判断是文件还是文件夹。
楼上的胡说,文件也可能没有扩展名,目录也可以有小数点
判断是文件还是文件夹
if(File.Exists(path)){
// 是文件
}else if(Directory.Exists(path)){
// 是文件夹
}else{
// 都不是
}
㈧ VC判断是否是文件夹
因为每个文件夹底下都有两个隐藏文件夹,一个是.,一个是..。这是每个文件夹创建时都会有的。.是代表本身这个文件夹,..是指上一级目录即父文件夹。