Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3398390
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: LINUX

2010-10-28 16:21:44

文件编程

Linux中文件编程可以使用两种方法:

Linux系统调用    C语言库函数

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

 系统调用-创建

int creat(const char *filename, mode_t mode)

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

mode:创建模式

常见创建模式:

S_IRUSR                      可读

S_IWUSR                      可写

S_IXUSR                     可执行

S_IRWXU                 可读、写、执行

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

v可执行 -> 1

v可写 -> 2

v可读 -> 4

v上述值的和,如可写可读 -> 6

v无任何权限 -> 0

 实例

#include

#include

#include

 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");

      }

     

      for(i=1;i

           create_file(argv[i]);

      }

     

      exit(EXIT_SUCCESS);

}

 

linux下编译如:

#  gcc  file_creat.c  –o  file_creat

   ./ file_creat    a

 文件描述

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

其增加至1024

系统调用-打开

vint open(const char *pathname, int flags)

vint 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来表示文件的访问权限。

系统调用-关闭

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

int close(int fd)

fd: 文件描述符,来源?

 

 

实例file_open.c

#include

#include

 

#include

#include

#include

 

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);   

}

系统调用-

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

功能:

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

 

系统调用-

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

功能:

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

 

系统调用-定位

int lseek(int fd, offset_t offset, int whence)

功能:

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

Whence可使用下述值:

SEEK_SET           相对文件开头

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

SEEK_END    相对文件末尾

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

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

 

系统调用-访问判断

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

int access(const char*pathname,int mode)

pathname:文件名称

mode:要判断的访问权限。可以取以下值或者是他们的组合。R_OK:文件可读,W_OK:文件可写,X_OK:文件可执行,F_OK文件存在。

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

例:

#include

int main()

{

if (access(“/etc/passwd”,R_OK) = =0)

printf(“/etc/passwd can be read!\n”);

 

综合实例file_cp.c

#include

#include

#include

#include

#include

 

#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);

}

库函数

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

 

库函数-创建和打开

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

ilename:

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

mode:

打开模式

常见打开模式:

 r, rb             只读方式打开

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

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

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

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

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

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

 

库函数-

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

功能:

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

 

库函数-

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

功能:

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

 

库函数-读字符

int fgetc(FILE *stream)

从指定的文件中读一个字符

#include

 

void main(vioid){

 

      FILE *fp;

      char ch;

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

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

           exit(1);

      }

      ch = fgetc(fp);

      while(ch!=EOF){

           putchar(ch);

           ch=fgetc(fp);

      }

      fclose(fp);

}

 

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

#include

void main(vioid){

 

      FILE *fp;

      char ch;

      if((fp=fopen("c1.txt","wt+"))==NULL){

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

           exit(1);

      }

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

      ch = getchar();

      while(ch!='\n'){

           fputc(ch,fp);

           ch=getchar();

      }

      printf("\n");

      fclose(fp);

}

 库函数-格式化读

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

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

#include

#include

int main(void)

{

      int i;

      printf("input an integer:");

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

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

      return 0;

 

}

 

库函数-格式化写

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

格式化输出到一个流中

#include

 

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);

}


库函数-定位

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

main()

{

      char buf[80];

      getcwd(buf,sizeof(buf));

      printf("current working directory:%s\n",buf);

}

 

创建目录

#include

int mkdir(char * dir, int mode)

功能:创建一个新目录。

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

 时间编程

时间类型

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

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

 时间获取

#include

time_t time(time_t *tloc)

功能:

获取日历时间,即从1970110点到现在所经历的秒数。

时间转化

 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

#include

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;

}

时间显示

char *asctime(const struct tm *tm)

功能:将tm格式的时间转化为字符串,如:

Sat Jul 30 08:43:03 2005

char *ctime(const time_t *timep)

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

 

例:time2.c

#include

#include

int main(void){

      struct tm *ptr;

      time_t it;

      it = time(NULL);

      ptr = gmtime(&it);

      printf(asctime(ptr));

      printf(ctime(&it));

      return 0;

}

获取时间

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

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

struct timeval {

int tv_sec; //秒数

int tv_usec; //微妙数

};

实例

#include

#include

#include

#include

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("Use Time:%f\n",timeuse);

      exit(0);

}

延时执行

unsigned int sleep(unsigned int seconds)

功能:使程序睡眠seconds秒。

void usleep(unsigned long usec)

功能:使程序睡眠usec微秒。

阅读(837) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~