c判断文件夹
举例来说:FILE*fp=fopen("dict.txt","r");charbuf[1024];if(fp!=(FILE*)NULL){while(fgets(buf,sizeof(buf),fp))//从文件中读入一行字符串,保存在buf中,直到读完所有字符串{//处理读入的字符串buf}fclose(fp);}
❷ c#中如何判断一个路径是目录还是文件
1、在visual studio当中创建一个C#控制台应用程序,选择新建项目,然后选择visual C#,再选中控制台应用程序,输入项目名称,选择位置,确定即可。
❸ C/C++判断文件/文件夹是否存在
一、判断文件夹是否存在: 1.用CreateDirectory(".//FileManege",NULL);如果文件夹FileManege不存在,则创建。 2.或者if(_access(".//FileManege",0)==-1),表示FileManege不存在。 3.或者BOOL PathIsDirectory(LPCTSTR pszPath);二、判断文件是否存在: 1.用if((file=fopen(".//FileManege//F//F.dat","rb"))==NULL) file=fopen(".//FileManege//F//F.dat","ab+"); // 先判断有无文件,没的话新建一个 2.用if(_access(".//FileManege//F//F.dat",0)==-1),表示文件不存在。 函数int _access( const char *path, int mode );可以判断文件或者文件夹的mode属性 mode=00;//Existence only mode=02;//Write permission mode=04;//Read permission 需要包含头文件<io.h>。
❹ 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
❺ 在C++中如何判断文件夹是否存在,不存在的话创建文件夹
参考代码如下:
#include <stdio.h>
#include <direct.h>
#include <stdlib.h>
#include <memory>
//检查文件夹是否存在,不存在则创建之
//文件夹存在返回 0
//文件夹创建失败返回-1
//文件夹创建失败返回1
int CheckDir(char* Dir)
{
FILE *fp = NULL;
char TempDir[200];
memset(TempDir,'\0',sizeof(TempDir));
sprintf(TempDir,Dir);
strcat(TempDir,"\\");
strcat(TempDir,".temp.fortest");
fp = fopen(TempDir,"w");
if (!fp)
{
if(_mkdir(Dir)==0)
{
return 1;//文件夹创建成功
}
else
{
return -1;//can not make a dir;
}
}
else
{
fclose(fp);
}
return 0;
}
❻ c判断文件夹是否存在
用 _access() 可销拦备以判断文件夹或文件是否存在。注意路径用衡滑双斜杠。例如:
#include <io.h>#include <stdio.h>#include <stdlib.h>main( ){ /* 检查存在否 */ if( (_access( "D:\\user\\亏毁C\\P1", 0 )) != -1 ) printf( "Yes !" );
else printf("No !");return 0;}
❼ c++ 判断文件夹是否存在,不存在则创建
c++中,<io.h>中的_access可以判断文件是否存在,<direct.h>中的_mkdir可以创建文件。
---------------------------------------------
建单级目录:
#include <io.h>
#include <direct.h>
#include <string>
int main()
{
std::string prefix = "G:/test/";
if (_access(prefix.c_str(), 0) == -1) //如果文件夹不存在
_mkdir(prefix.c_str()); //岩闹尺则创建
}
----------------------------------------------------
建多级目录:
最后一个如果是文件粗高夹的话,需要加上 '\\' 或者 '/'
#include <io.h>
#include <direct.h>
#include <string>
int createDirectory(std::string path)
{
int len = path.length();
char tmpDirPath[256] = { 0 };
for (int i = 0; i < len; i++)
{
tmpDirPath[i] = path[i];
if (tmpDirPath[i] == '\\'弯芦 || tmpDirPath[i] == '/')
{
if (_access(tmpDirPath, 0) == -1)
{
int ret = _mkdir(tmpDirPath);
if (ret == -1) return ret;
}
}
}
return 0;
}
❽ c语言怎么判定一个文件夹是否有文件谢谢
#include<携察dos.h>
#include<dir.h>
void main()
{
struct ffblk ffblk;
int success=0; //假设没有文件
int done;
done = findfirst("c:\\test\\空坦*.*"辩亏茄,&ffblk,0); //假设文件夹名称c:\\test\\
if(done==0)
success=1; //表示有文件
}
❾ 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++ 怎样判断文件夹是否为空
c++判断文本文件为空,必须要先读一下文件(读一个字符),才能判断是否为空。
刚打开文件时,文件状态标识都是初始化状态,eof()函数是根据文件状态标识来判断当前是否到了文件尾,而不是用来判断文件是否为空。当读取文件数据时,遇到文件尾时,系统会更改文件状态标识为文件尾,同时,返回EOF标志。
参考代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream file;
int ch;
file.open("a.txt",ios::in);
ch=file.get(); //试图去读一个字符
if(file.eof()) //或 if ( ch==EOF ) //一个字符都未读到,表示文件为空
{
cout<<"文件为空"<<endl;
}
file.close();