1.
int access(const char *filename, int amode);
amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1。
这个函数还可以检查其它文件属性:
06 检查读写权限
04 检查读权限
02 检查写权限
01 检查执行权限
00 检查文件的存在性
而这个就算这个文件没有读权限,也可以判断这个文件存在于否
存在返回0,不存在返回-1
C函数
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
[编辑本段]access
Synopsis
#include <io.h>
int _access(const char *path,int mode) ;
Description
The access function, when used with files, determines whether thespecified file exists and can be accessed as specified by the value ofmode. When used with directories, _access determines only whether thespecified directory exists; since under Windows all directories haveread and write access.
The mode argument can be one of :
00 Existence only
02 Write permission
04 Read permission
06 Read and write permission
Returns
Zero if the file has the given mode, -1 if an error occurs.
Portability :
Windows. Under Unix a similar function exists too.
Note that lcc-win32 accepts both _access (Microsoft convention) and access.
程序例:
#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);
}
2.
字符串截取
#include "stdio.h"
#include "conio.h"
/*从左边开始截取字符串 */
char *left(char *dst,char *src,int n,int m)
{
char *p=src;
char *q=dst;
int len=strlen(src);
if(n>len) n=len;
while(n--) *(q++)=*(p++);
*(q++)='\0';
return dst;
}
/* 从右边开始截取*/
char *right(char *dst,char *src,int n,int m)
{
char *p=src;
char *q=dst;
int len=strlen(src);
if(n>len) n=len;
p+=(len-n);
while(*(q++)=*(p++));
return dst;
}
/*从中部截取字符串 */
char *mid(char *dst,char *src,int n,int m)
{
char *p=src;
char *q=dst;
int len=strlen(src);
if(n>len) n=len-m;
if(m>0) return NULL;
p += m;
while(n--) *(q++) = *(p++);
*(q++)='\0';
return dst;
}
char *substring(char *dst,char *src,int len,int start)
{
char *p=dst;
char *q=src;
int length=strlen(src);
if(start>=length||start<0) return NULL;
if(len>length) len=length-start;
q+=start;
while(len--)
{
*(p++)=*(q++);
}
*(p++)='\0';
return dst;
}
main()
{
char *src="411524198510088017";
char *year,*month,*day;
int i;
/* 截取年
left(year,src,4,0);
for(i=0;i<strlen(year);i++)
{
printf("%c",*(year+i));
}
printf("Hello, world\n");
*/
substring(year,src,4,6);
for(i=0;i<strlen(year);i++)
{
printf("%c",*(year+i));
}
getch();
}
原型:extern char *strstr(char *haystack, char *needle);
用法:#include <string.h>
功能:从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL)。
说明:返回指向第一次出现needle位置的指针,如果没找到则返回NULL。
举例:
// strstr.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char *l="lob";
char *p;
clrscr();
p=strstr(s,l);
if(p)
printf("%s",p);
else
printf("Not Found!");
getchar();
return 0;
}