LINUX 文件、时间编程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/snaking616/article/details/80026873
目录:
1. 系统调用-文件访问
  1.1 系统调用-创建
  1.2 系统调用-打开与关闭
  (1)文件描述
  (2)系统调用-打开
  (3)系统调用-关闭
  1.3 系统调用-读与写
  (1)系统调用-读  
  (2)系统调用-写 
  1.4 系统调用-定位
  1.5 系统调用-访问判断
2. 库函数-文件访问
  2.1 库函数-创建和打开 
  2.2 库函数-读与写
  (1)库函数-读
  (2)库函数-写
  2.3 库函数-读字符与写字符
  (1)库函数-读字符
  (2)库函数-写字符
  2.4 库函数-格式化读与写
  (1)库函数-格式化读
  (2)库函数-格式化写
  2.5 库函数-定位

3. 时间编程

  3.1 时间类型
  3.2 时间获取
  3.3 时间转化
  3.4 时间保存
  3.5 时间获取
  3.6 时间显示
  3.7 获取时间
  3.8 延时执行

文件编程

  Linux中文件编程可以使用两种方法:Linux系统调用、C语言库函数,前者依赖于Linux系统,后者与操作系统是独立的,在任何操作系统下,使用C语言库函数操作文件的方法都是相同的。


1. 系统调用-文件访问
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 

 实例分析(演示):  file_creat.c 

#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
void create_file(char *filename){ 
/*创建的文件具有什么样的属性?*/ 
    if(creat(filename,0755)<0){ 
        printf("create file %s failure!\n",filename); 
        exit(EXIT_FAILURE); 
    }else{ 
        printf("create file %s success!\n",filename); 
    } 
} 
int main(int argc,char *argv[]){ 
    int i; 
    if(argc<2){ 
        perror("you haven't input the filename,please try again!\n"); 
        exit(EXIT_FAILURE); 
    } 
    for(i=1;i<argc;i++){ 
        create_file(argv[i]);    
    } 
    exit(EXIT_SUCCESS); 
}

1.2 系统调用-打开与关闭

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

(2)系统调用-打开

  int open(const char *pathname, int flags)

  int open(const char *pathname, int flags, 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来表示文件的访问权限。

  实例分析(演示): file_open.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc ,char *argv[]){
    int fd;
    if(argc<2){
        puts("please input the open file pathname!\n");
        exit(1);
    }
    //如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755
    //如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错.
    //所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件
    if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0){
        perror("open file failure!\n");
        exit(1);
    }else{
        printf("open file %d  success!\n",fd);
    }
    close(fd);
    exit(0);
}

(3)系统调用-关闭

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

  int close(int fd)

  fd: 文件描述符,来源?

1.3 系统调用-读与写

(1)系统调用-读  

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

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

(2)系统调用-写  

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

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

1.4 系统调用-定位

  功能:
  将文件读写指针相对whence移动offset个字节。操作成功时,返回文件指针相对于文件头的位置。
  whence可使用下述值:
  SEEK_SET:相对文件开头
  SEEK_CUR:相对文件读写指针的当前位置
  SEEK_END:相对文件末尾

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

  lseek(fd, -5, SEEK_CUR)  

  如何利用lseek来计算文件长度?

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

  lseek(fd, 0, SEEK_END)。  

1.5 系统调用-访问判断

  有时我们需要判断文件是否可以进行某种操作(读,写,等),这时可以使用access函数:
  int access(const char*pathname,int mode)
  pathname:文件名称
  mode:要判断的访问权限。可以取以下值或者是他们的组合。R_OK:文件可读,W_OK:文件可写,X_OK:文件可执行,F_OK文件存在。

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

  例:

#include<unistd.h>
int main()
{
if (access(“/etc/passwd”,R_OK) = =0)
printf(“/etc/passwd can be read!\n”);
}

综合实例(演示):file_cp.c

#include <sys/types.h>
#include <sys/stat.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); 
} 

2. 库函数-文件访问
  库函数:

  C库函数的文件操作是独立于具体的操作系统平台的,不管是在DOS、Windows、Linux还是在VxWorks中都是这些函数。

2.1 库函数-创建和打开  

  FILE *fopen(const char *filename, const char *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 库函数-读与写

(1)库函数-读

  size_t fread(void *ptr, size_t size, size_t n, FILE *stream)
  功能: 从stream指向的文件中读取n个字段,每个字段为size字节,并将读取的数据放入ptr所指的字符数组中,返回实际已读取的字节数。
(2)库函数-写
  size_t fwrite (const void *ptr, size_t size, size_t n, FILE *stream)
  功能: 从缓冲区ptr所指的数组中把n个字段写到stream指向的文件中,每个字段长为size个字节,返回实际写入的字段数。

2.3 库函数-读字符与写字符

(1)库函数-读字符

  int fgetc(FILE *stream)
  从指定的文件中读一个字符
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("c1.txt","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}

(2)库函数-写字符

  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 any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
ch=getchar();
while (ch!='\n') {
fputc(ch,fp);
ch=getchar();
}
printf("\n");
fclose(fp);
}

2.4 库函数-格式化读与写

(1)库函数-格式化读

  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)库函数-格式化写
  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", fp );
fclose( stream );
 }

2.5 库函数-定位

  int fseek(FILE *stream, long offset, int whence)
  whence :
  SEEK_SET 从文件的开始处开始搜索
  SEEK_CUR 从当前位置开始搜索
  SEEK_END 从文件的结束处开始搜索
  路径获取
  在编写程序的时候,有时候需要得到当前路径。C库函数提供了getcwd来解决这个问题。
  char *getcwd(char *buffer,size_t size)

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

#include<unistd.h>
main()
{
char buf[80];
getcwd(buf,sizeof(buf));
printf(“current working directory : %sn”,buf);
}

  创建目录
  #include <sys/stat.h>
    int mkdir(char * dir, int mode)
功能:创建一个新目录。
返回值:0表示成功,-1表述出错。
使用库函数实现file_cp的功能

实验一: 使用库函数实现file_cp的功能。

3. 时间编程

3.1 时间类型

  Coordinated Universal Time(UTC):世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。
  Calendar Time:日历时间,是用“从一个标准时间点(如:1970年1月1日0点)到此时经过的秒数”来表示的时间。

3.2 时间获取

  #include <time.h>
    time_t time(time_t *tloc)

功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数。
备注:/* typedef long time_t */

3.3 时间转化

  struct tm *gmtime(const time_t *timep)
  功能:将日历时间转化为格林威治标准时间,并保存至TM结构。
  struct tm *localtime(const time_t *timep)

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

3.4 时间保存

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; //日光节约时间
};

3.5 时间获取

例: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.6 时间显示
  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.7 获取时间

int gettimeofday(struct timeval *tv,struct timezone *tz)
功能:获取从今日凌晨到现在的时间差,常用于计算事件耗时。
struct timeval {
int tv_sec; //秒数
int tv_usec; //微妙数
};

实例分析(演示)

Time3.c

#include <sys/time.h> 
#include <stdio.h>
#include <stdlib.h> 
#include <math.h>
/* 算法分析 */
void function() 
{ 
unsigned int i,j; 
double y; 
for(i=0;i<1000;i++) 
for(j=0;j<1000;j++) 			
y++; 
} 
main() 
{ 	
struct timeval tpstart,tpend; 	
float timeuse; 	
gettimeofday(&tpstart,NULL); // 开始时间
function(); 	
gettimeofday(&tpend,NULL);   // 结束时间
/* 计算执行时间 */	
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ 
	tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000; 	
printf("Used Time:%f\n",timeuse); 	
exit(0); 
} 

3.8 延时执行

  unsigned int sleep(unsigned int seconds)

  功能:使程序睡眠seconds秒。
  void usleep(unsigned long usec)
  功能:使程序睡眠usec微秒。

实验二:
获取本地时间,以字符串方式显示


猜你喜欢

转载自blog.csdn.net/snaking616/article/details/80026873
今日推荐