学习笔记二之文件与时间编程

 

Linux中文件编程可以使用两种方法:Linux系统调用,C语言库函数

前者依赖于Linux系统,后者与操作系统是独立的,在任何操作系统下,使用C语言库函数操作文件的方法都是相同的。

1.1系统调用-创建

 

int creat(const char *filename,mode_t mode)

filename:要创建的文件名(包含路径,缺省为当前路径)

mode:创建模式

常见创建模式:

S_IRUSR 可读         S_IWUSR 可写

S_IXUSR 可执行      S_IRWXU 可读、写、执行

除了可以使用上述宏以外,还可以直接使用数字来表示文件的访问权限:

可执行-> 1           可写->2            可读-> 4

上述值的和,如可写可读-> 6        无任何权限-> 0

在Linux系统中,所有打开的文件都对应一个文件描述符。文件描述符的本质是一个非负整数。当打开一个文件时,该整数由系统来分配。文件描述符的范围是0 - OPEN_MAX 。早期的UNIX版本OPEN_MAX =19,即允许每个进程同时打开20个文件,现在很多系统则将其增加至1024。

1.2系统调用-打开

 

int open(const char *pathname, int flags)

int open(const char *pathname, intflags,mode_t mode)

pathname:要打开的文件名(包含路径,缺省为当前路径)

flags:打开标志

常见的打开标志:

O_RDONLY 只读方式打开            O_WRONLY 只写方式打开

O_RDWR 读写方式打开                O_APPEND 追加方式打开

O_CREAT 创建一个文件                O_NOBLOCK 非阻塞方式打开

如果使用了O_CREATE标志,则使用的函数是:int open(const char *pathname,int flags,mode_t mode);这时需要指定mode来表示文件的访问权限

当我们操作完文件以后,需要关闭文件:

int close(int fd)

fd: 文件描述符,来源?

1.3系统调用-读

 

 intread(int fd, const void *buf, size_t length)

功能:

从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中,返回值为实际读取的字节数。

1.4系统调用-写

 

 intwrite(int fd, const void *buf, size_t length)

功能:

把length个字节从buf指向的缓冲区中写到文件描述符fd所指向的文件中,返回值为实际写入的字节数。

1.5系统调用-定位

 

int lseek(int fd, offset_t offset, intwhence)

功能:

将文件读写指针相对whence移动offset个字节。操作成功时,返回文件指针相对于文件头的位置。

whence可使用下述值:

SEEK_SET:相对文件开头

SEEK_CUR:相对文件读写指针的当前位置

SEEK_END:相对文件末尾

offset可取负值,表示向前移动。例如下述调用,可将文件指针相对当前位置向前移动5个字节:lseek(fd, -5, SEEK_CUR)

由于lseek函数的返回值为文件指针相对于文件头的位置,因此下面调用的返回值就是文件的长度:

lseek(fd, 0, SEEK_END)

1.6系统调用-访问判断

 

有时我们需要判断文件是否可以进行某种操作(读,写等),这时可以使用access函数:

int access(const char*pathname,int mode)

pathname:文件名称

mode:要判断的访问权限。可以取以下值或者是他们的组合。

R_OK:文件可读,W_OK:文件可写,

X_OK:文件可执行,F_OK文件存在。

返回值:当我们测试成功时,函数返回0,否则如果一个条件不符时,返回-1。

2.1库函数-创建和打开

 

FILE *fopen(const char *filename, constchar *mode)

filename:打开的文件名(包含路径,缺省为当前路径)

mode:打开模式

常见打开模式:

 r,rb 只读方式打开

 w,wb 只写方式打开,如果文件不存在,则创建该文件

 a,ab 追加方式打开,如果文件不存在,则创建该文件

 r+,r+b, rb+ 读写方式打开

 w+,w+b, wh+ 读写方式打开,如果文件不存在,则创建该文件

a+, a+b, ab+ 读和追加方式打开。如果文件不存在,则创建该文件

b用于区分二进制文件和文本文件,这一点在DOS、Windows系统中是有区分的,但Linux不区分二进制文件和文本文件

2.2库函数-读

 

size_t fread(void *ptr, size_t size, size_tn, FILE*stream)

功能:

从stream指向的文件中读取n个字段,每个字段为size字节,并将读取的数据放入ptr所指的字符数组中,返回实际已读取的字节数

2.3库函数-写
 

size_t fwrite (const void *ptr, size_tsize, size_t n,FILE *stream)

功能:

从缓冲区ptr所指的数组中把n个字段写到stream指向的文件中,每个字段长为size个字节,返回实际写入的字段数。

2.4库函数-读字符

 

int fgetc(FILE *stream)从指定的文件中读一个字符

#include<stdio.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("c1.txt","rt"))==NULL)

{

printf("\nCannot open file strike anykey exit!");

getch();

exit(1);

}

ch=fgetc(fp);

while(ch!=EOF)

{

putchar(ch);

ch=fgetc(fp);

}

fclose(fp);

}

 

2.5库函数-写字符

 

int fputc(int c, FILE *stream)向指定的文件中写入一个字符

#include<stdio.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("string","wt+"))==NULL){

printf("Cannot open file,strike anykey exit!");

getch();

exit(1);

}

printf("input a string:\n");

ch=getchar();

while (ch!='\n') {

fputc(ch,fp);

ch=getchar();

}

printf("\n");

fclose(fp);

}

 

2.6库函数-格式化读

 

fscanf(FILE *stream, char*format[,argument...])

从一个流中进行格式化输入

#include <stdlib.h>

#include <stdio.h>

int main(void)

{

int i;

printf("Input an integer: ");

if (fscanf(stdin, "%d", &i))

printf("The integer read was:%i\n", i);

return 0;

}

 

2.7库函数-格式化写

 

int fprintf(FILE *stream, char*format[,argument,...])

格式化输出到一个流中

#include <stdio.h>

#include <process.h>

FILE *stream;

void main( void )

{

int i = 10;

double fp = 1.5;

char s[] = "this is a string";

char c = '\n';

stream = fopen( "fprintf.out","w" );

fprintf( stream, "%s%c", s, c );

fprintf( stream, "%d\n", i );

fprintf( stream, "%f\n",

 

2.8库函数-定位

 

int fseek(FILE *stream, long offset, intwhence)

whence :

SEEK_SET 从文件的开始处开始搜索

SEEK_CUR 从当前位置开始搜索

SEEK_END 从文件的结束处开始搜索

2.9路径获取

 

在编写程序的时候,有时候需要得到当前路径。C库函数提供了getcwd来解决这个问题。

char *getcwd(char *buffer,size_t size)

我们提供一个size大小的buffer,getcwd会把当前的路径名copy 到buffer中.如果buffer太小,函数会返回-1。

2.10创建目录

 

#include <sys/stat.h>

int mkdir(char * dir, int mode)

功能:创建一个新目录。

返回值:0表示成功,-1表述出错。

 

3时间类型

 

Coordinated Universal Time(UTC):世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。

 Calendar Time:日历时间,是用“从一个标准时间点(如:1970年1月1日0点)到此时经过的秒数”来表示的时间。

3.1时间获取

 

#include <time.h>

time_t time(time_t *tloc)

功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数。

/* typedef long time_t */

3.2时间转化

 

struct tm *gmtime(const time_t *timep)

功能:将日历时间转化为格林威治标准时间,并保存至TM结构。

struct tm *localtime(const time_t *timep)

功能:将日历时间转化为本地时间,并保存至TM结构。

struct tm {

int tm_sec; //秒值

int tm_min; //分钟值

int tm_hour; //小时值

int tm_mday; //本月第几日

int tm_mon; //本年第几月

int tm_year; //tm_year + 1900 = 哪一年

int tm_wday; //本周第几日

int tm_yday; //本年第几日

int tm_isdst; //日光节约时间

};

//例:time1.c (演示)

#include <time.h>

#include <stdio.h>

int main(void) {

struct tm *local;

time_t t;

t=time(NULL);

local=localtime(&t);

printf("Local hour is:%d\n",local->tm_hour);

local=gmtime(&t);

printf("UTC hour is:%d\n",local->tm_hour); return 0;

}

 

3.3时间显示

 

char *asctime(const struct tm *tm)

功能:将tm格式的时间转化为字符串,如:Sat Jul 30 08:43:03 2005

char *ctime(const time_t *timep)

功能:将日历时间转化为本地时间的字符串形式

//例:time2.c (演示)

#include <time.h>

#include <stdio.h>

int main(void)

{

struct tm *ptr;

time_t lt;

lt=time(NULL);

ptr=gmtime(<);

printf(asctime(ptr));

printf(ctime(<));

return 0;

}

 

3.4获取时间

 

int gettimeofday(struct timeval *tv,structtimezone *tz)

功能:获取从今日凌晨到现在的时间差,常用于计算事件耗时。

struct timeval {

int tv_sec; //秒数

int tv_usec; //微妙数

};

3.5延时执行

 

unsigned int sleep(unsigned int seconds)

功能:使程序睡眠seconds秒。

void usleep(unsigned long usec)

功能:使程序睡眠usec微秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值