c语言文件存在函数 c语言保存文件函数

C语言判断指定文件是否存在

头文件:io.h

目前创新互联公司已为近1000家的企业提供了网站建设、域名、网络空间、网站托管、企业网站设计、下花园网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

功 能: 确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。如果指定的存取方式有效,则函数返回0,否则函数返回-1。

用 法: int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );

参数说明:

filenpath

文件或文件夹的路径,当前目录直接使用文件或文件夹名

备注:当该参数为文件的时候,access函数能使用mode参数所有的值,当该参数为文件夹的时候,access函数值能判断文件夹是否存在。在WIN NT 中,所有的文件夹都有读和写权限

mode

要判断的模式

在头文件unistd.h中的预定义如下:

#define R_OK 4 /* Test for read permission. */

#define W_OK 2 /* Test for write permission. */

#define X_OK 1 /* Test for execute permission. */

#define F_OK 0 /* Test for existence. */

具体含义如下:

00 只判断是否存在

02 只判断是否有写权限

04 只判断是否有读权限

06 判断是否有读并且有写权限

程序例

#includestdio.h

#includeio.h

int file_exists(char *filename);

int main(void)

{

printf("Does NOTEXIST.FIL exist: %s\n",

file_exists("NOTEXISTS.FIL") ?"YES":"NO");

return 0;

}

int file_exists(char *filename)

{

return (access(filename, 0) == 0);

}

头文件:io.h

功 能: 确定文件或文件夹的访问权限。即,检查某个文件的存取方式,比如说是只读方式、只写方式等。如果指定的存取方式有效,则函数返回0,否则函数返回-1。

用 法: int access(const char *filenpath, int mode); 或者int _access( const char *path, int mode );

参数说明:

filenpath

文件或文件夹的路径,当前目录直接使用文件或文件夹名

备注:当该参数为文件的时候,access函数能使用mode参数所有的值,当该参数为文件夹的时候,access函数值能判断文件夹是否存在。在WIN NT 中,所有的文件夹都有读和写权限

mode

要判断的模式

在头文件unistd.h中的预定义如下:

#define R_OK 4 /* Test for read permission. */

#define W_OK 2 /* Test for write permission. */

#define X_OK 1 /* Test for execute permission. */

#define F_OK 0 /* Test for existence. */

具体含义如下:

00 只判断是否存在

02 只判断是否有写权限

04 只判断是否有读权限

06 判断是否有读并且有写权限

程序例

#includestdio.h

#includeio.h

int file_exists(char *filename);

int main(void)

{

printf("Does NOTEXIST.FIL exist: %s\n",

file_exists("NOTEXISTS.FIL") ?"YES":"NO");

return 0;

}

int file_exists(char *filename)

{

return (access(filename, 0) == 0);

}

C语言文件函数

//要另外说下如fprintf(stderr, "Can't open %s\n", file_app);这是向文件或者系统设备输出的函数;但他的文件指针为stderr;这是c中的标准错误输出设备指针,系统自动分配为显示器故相当于printf("Can't open %s\n", file_app);

#include stdio.h

#include stdlib.h

#include string.h

#define BUFSIZE 1024

#define SLEN 81

void append(FILE *source, FILE *dest);

int main(void)

{

FILE *fa, *fs; //定义2个文件类型指针

int files = 0; // 追加文件个数

char file_app[SLEN];

char file_src[SLEN]; // 2个字符串用来储存文件名;

puts("Enter name of destination file:");//输出Enter name of destination file:

gets(file_app);//输入要追加的文件名

if ((fa = fopen(file_app, "a")) == NULL)//fa指向追加的目的文件,以追加方式打开文件,如果打开失败退出;

{

fprintf(stderr, "Can't open %s\n", file_app);

exit(2);

}

if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)//创建缓冲器与流相关,大小为BUFSIZE,作用是提高IO速度;如果打开失败退出

{

fputs("Can't create output buffer\n", stderr);

exit(3);

}

puts("Enter name of first source file (empty line to quit):");//输出Enter name of first source file (empty line to quit):

while (gets(file_src) file_src[0] != '\0')//输入源文件如果是空串结束循环

{

if (strcmp(file_src, file_app) == 0)//如果源和追加文件相同

fputs("Can't append file to itself\n",stderr);

else if ((fs = fopen(file_src, "r")) == NULL)//如果打开源文件失败

fprintf(stderr, "Can't open %s\n", file_src);

else

{

if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)//创建缓冲器与流相关,大小为BUFSIZE,作用是提高IO速度;如果打开失败开始下次循环

{

fputs("Can't create input buffer\n",stderr);

continue;

}

append(fs, fa);//函数

if (ferror(fs) != 0)//检查文件操作是否有错

fprintf(stderr,"Error in reading file %s.\n",

file_src);

if (ferror(fa) != 0)

fprintf(stderr,"Error in writing file %s.\n",

file_app);

fclose(fs);//关闭源文件

files++;//追加文件数+1

printf("File %s appended.\n", file_src);

puts("Next file (empty line to quit):");

}

}

printf("Done. %d files appended.\n", files);

fclose(fa);//关闭追加文件

return 0;

}

void append(FILE *source, FILE *dest)

{

size_t bytes;

static char temp[BUFSIZE];

while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) 0)//把源文件的内容追加到追加文件,块大小sizeof(char),块数为BUFSIZE

fwrite(temp, sizeof (char), bytes, dest);//写文件块大小sizeof(char),块数为BUFSIZE

}

判断文件是否存在 c语言 函数

排版後如下: int access(const char* szPathName){ #ifdef _WIN32 if ( _access(szPathName, 0) != 0 ) return -1; #else if ( access(szPathName, F_OK) != 0 ) return -1; #endif return 0; } 這是用到 preprocessor. 用在跨平台(platform)的各種編譯器. 各平台的 .h 會不同. 在微軟的平台會定義 _WIN32 這個符號. 所以這段話就成為 int access(const char* szPathName){ if ( _access(szPathName, 0) != 0 ) return -1; return 0; } 在其它平台不會定義 _WIN32 這個符號. 所以這段話就成為 int access(const char* szPathName){ if ( access(szPathName, F_OK) != 0 ) return -1; return 0; }

本文名称:c语言文件存在函数 c语言保存文件函数
当前路径:https://www.cdcxhl.com/article24/hjpije.html

成都网站建设公司_创新互联,为您提供关键词优化品牌网站设计外贸网站建设响应式网站搜索引擎优化面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设