系统调用-文件访问
Linux中编程可以使用两种方式:
- Linux 系统调用
- C语言库函数
1.系统调用创建
int creat(const char *filename,mode_t mode)
filename:要创建的文件名(包含路径,缺省为当前路径)
mode:创建模式
mode常见模式:
int creat(const char *filename,mode_t mode)
- filename:要创建的文件名(包括路径,缺省为当前路径)
- mode:创建模式
除了可以使用上述宏以外,还可以直接使用数字表示文件到额访问权限 - 可执行 -》1
- 可写 -》2
- 可读 -》4
- 上述值和,如可读可写 -》6
- 无任何权限 -》0
4位长的数据分别代表什么:0755 “ 0 所有者,所在的组,其他用户”
例程代码:
对创建文件的时候使用的那个循环表示不太理解!
yourexe.exe yourfile
argc 2
argv[0] 指向 "yourexe.exe"
argv[1] 指向 "yourfile"
创建一个名为app的文件!
2.文件的描述
本质上是一个非负的整数,
3.系统调用 -打开
int open(const char *pathname,int flags)
int open(const char *pathname,int flags,mode_t mode)
//pathname:要打开的文件名
//flags:打开标志
常见的打开标志
- O_RDDNLY 只读方式打开
- O_WRONLY 只写方式打开
- O_RDWR 读写方式打开
- O_APPEND 追加方式打开
- O_CREAT 创建一个文件 使用第二个
- O_NOBLOCK非阻塞方式打开
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> // for close
int main(int argc,char *argv[])
{
int fd;
if(argc<2)
{
printf("please input the open file pathname");
exit(1);
}
if((fd=open(argv[1],O_CREAT|O_RDWR,0775))<0)
{
perror("open file failure!\n");
exit(1);
}
else
{
printf("open file %d success!\n",fd);
}
close(fd);
exit(0);
}
~
[编译错误:implicit declaration of function ‘close’]
添加 #include <unistd.h> 头文件
4.系统调用 -关闭
int close(int fd);
5.系统调用 -读
int read(int fd ,const void *buf,size_t length)
从文件描述符fd所指定的文件中读取lenth个字节到buf所指向的缓冲区中,返回值为实际的字节数
6.系统调用 -写
int write(int fd ,const void *buf,size_t length)
把length个字节从buf指向的缓冲区中写到文件描述符fd所指向的文件中,返回实际写入的值
7.系统调用 移动指针
int lseek(int fd ,offset_t offset,int whence)
fd:操作的文件
offset:移动多少距离(例如:-4)
whence:从什么地方开始移动指针
返回值:文件指针相对于文件尾的位置
whence可使用下述值:
- SEEK_SET:相对文件开头
- SEEK_CUR:相对文件读写指针的当前位置
- SEEK_END:相对文件末尾
计算文件的长度:
lseek(fd ,0,SEEK_END);
7.系统调用 访问判断
int access(const char *pathname,int mode)
pathname:文件名称
mode:要判断的访问权限。可以取以下值或者是他们的组合
R_OK:文件可读
W_OK:文件可写
X_OK:文件可执行
F_OK:文件存在
返回值:正确:0,不正确-1
对文件进行创建
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> // for close
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_SIZE];
char *ptr;
if(argc!=3)
{
fprintf(stderr,"Usage:%s fromfile tofile/n/a",argv[0]);
exit(1);
}
if((from_fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Open %s Error:%s/n",argv[1],strerror(errno));
exit(1);
}
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s/n",argv[2],strerror(errno));
exit(1);
}
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
if((bytes_read==-1)&&(errno!=EINTR)) break;
else if(bytes_read>0)
{
ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
if ((bytes_write==-1)&&(errno!=EINTR)) break;
else if(bytes_write==bytes_read) break;
else if(bytes_write>0)
{
ptr+=bytes_write;
bytes_read-=bytes_write;
}
}
if(bytes_write==-1) break;
}
}
close(from_fd);
close(to_fd);
exit(0);
}
运行结果:
库函数-文件访问
时间编程
时间获取
#include <time.h>
time_t time(time_t *tloc)
NULL
功能,获取日历时间,即从1970年1月1日零点到所在所经历的秒数
#include <time.h>
int gettimeofday(struct timeval *tv,struct timezone *tz)
功能:获取从今日凌晨到现在的时间差,用于计算事件耗时
时间显示
char *asctime(const struct tm *tm)
功能:将tm格式的时间转化成字符串,如:Sat Jul 30 08:
char *ctime(const time_t *timep)
功能:将日历时间转化为本地时间的字符串形式