************************************************************************************************************
创建文件夹,在在文件里面创建文件:
一个实例:
#include <sys/stat.h>
#include<stdio.h>#include<time.h>
#include<sys/types.h>
int main()
{
char txtname[100];
*txtname='005';//这句将结果转变为字符串
if(access("/root/zy/telnet/flow",0)==-1)//access函数是查看文件是不是存在
{
if (mkdir("/root/zy/telnet/flow",0777))//如果不存在就用mkdir函数来创建
{
printf("creat file bag failed!!!");
}
}
char pathname[100];
pathname[0]='f';
pathname[1]='l';
pathname[2]='o';
pathname[3]='w';
pathname[4]='/';
pathname[5]='/';
int i;
for(i=0;txtname[i]!='\0';i++)
{
int j=6+i;
pathname[j]=txtname[i];
}
pathname[i+6]='.'; //这几句是加上后缀 .txt的
pathname[i+7]='t';
pathname[i+8]='x';
pathname[i+9]='t';
pathname[i+10]='\0';//最后别忘记加上这个
FILE *fp;
if((fp=fopen(pathname,"w"))==NULL)//打开文件 没有就创建
{
printf("文件还未创建!\n");
}
fprintf(fp,"创建成功");
fclose(fp);
return 0;
}
编译一个后有警告:
CreateFile.c: In function ‘main’:
CreateFile.c:10:16: warning: multi-character character constant
CreateFile.c:10:5: warning: overflow in implicit constant conversion
修改:
char txtname[100];
*txtname='005';//这句将结果转变为字符串
改为:
char txtname[100] = “005”;
access()函数的使用:无印证
头文件:unistd.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. */
具体含义如下:
R_OK 只判断是否有读权限
W_OK 只判断是否有写权限
X_OK 判断是否有执行权限
F_OK 只判断是否存在
access函数程序范例(C语言中)
#include <stdio.h>
#include <io.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);
}