Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322899
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 321
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-04 16:09
文章分类

全部博文(124)

文章存档

2014年(124)

分类: 嵌入式

2014-08-13 19:31:50

文件IO不带缓存,每个read和write都调用内核中的相应系统调用。
文件IO常用函数:
open,close,read,write,lseek
对于内核而言,所有打开文件都有文件描述符引用。
文件描述符是一个非负整数。当打开一个现存文件或创建一个新文件时,你诶和向进程返回一个文件描述符。
当读、写一个文件时,用open返回的文件描述符标识该文件,将其作为参数传给read或write。


1.open(被打开的文件名(可包含文件路径),int flag, mode)
falg : O_RDONLY,O_WDONLY,O_RDWR,O_CREAT,O_EXCL(如果存在返回错误信息)
O_TRUNC(如果已存在,则删除文件中数据)
2.read(fd,buf,size_t count)
调用成功返回读取的字节数。
如果返回0,表示到达文件的末尾。
如果返回-1,表示出错,通过errno设置错误码。
3.write()
4.lseek(fd,offset,whence)

5.打开文件目录opendir()

6.获取文件属性函数:这组函数还蛮重要的。
stat()获取一个于此命名文件有关的信息结构
fstat()获得已在描述符filedes上打开的文件的有关信息
lstat()返回该符号链接的有关信息,而不是有该符号链接引用的文件信息

stat内结构体中参数:st_mode ,st_mode 是被打开文件的属性描述。

S_IFMT : 0x070000

switch(st.st_mode & S_IFMT) //判断是何种类型的文件
 {
 case S_IFREG:  printf("-"); break;
 case S_IFDIR:  printf("d"); break;
 case S_IFLNK:  printf("l"); break;
 case S_IFBLK:  printf("b"); break;
 case S_IFCHR:  printf("c"); break;
 case S_IFIFO:  printf("p"); break;
 case S_IFSOCK: printf("s"); break;
 }

S_...是内部定义的一些宏,具体man一下看看

7.getopt函数查一下自己理解,getopt的作用是识别以“-”开头的字符。

while ((ch = getopt(argc, argv, "la")) != EOF)
 {
  switch ( ch )
  {
  case 'a' :
   printf("option a is set\n");
   aflag = 1;
   break;
  case 'l' :
   printf("option l is set\n");
   lflag = 1;
   break;
  default :
   printf("wrong option %c is set\n", optopt);
  }
 }

以及getpwuid(st.st_uid);getgrgid(st.st_gid);函数,分别是获得当前用户名,用户组名

8.strtok()函数,是截断字符串的操作。以下的例子是利用空格来截断。

这个函数比较怪异,第一次赋值的时候必须是buf,而以后的第一个参数是NULL,这个比较恶心....

arg[i++] = strtok(buf, " ");
            do
            {
                arg[i++] = strtok(NULL, " ");
            }while(arg[i-1] != NULL);

9.关于文件夹操作的函数: DIR *opendir();struct dirent * readdir():内部有指针,可以自动移动文件夹中下一个文件,直到为空)

 

复制代码
例子:利用他们来完成文件的拷贝

#include  #include  #include  #include  #include <string.h> int main(int argc, char **argv)
{ int fd1, fd2; char buf[1024]; int nbyte; if(argc != 3)
    {
        printf("Using : %s srcfilename decfilename\n", argv[0]); return -1;
    } if((fd1 = open(argv[1], O_RDONLY)) < 0)
    {
        perror("open"); return -1;
    } if((fd2 = open(argv[2], O_WRONLY| O_CREAT | O_TRUNC, 0666))  < 0)
    {
        perror("open"); return -1;
    } while((nbyte = read(fd1, buf, sizeof(buf))) > 0)
    {
        write(fd2, buf, nbyte);
    }

    close(fd1);
    close(fd2); return 0;

}

打开文件目录opendir()

#include  #include  #include  int main(int argc, char **argv)
{
    DIR *dir; struct dirent *dirent;

    dir = opendir(argv[1]); while((dirent = readdir(dir)) != NULL)
    {
        printf("%s\n", dirent->d_name);
    }
}

获取文件属性函数:
stat()获取一个于此命名文件有关的信息结构
fstat()获得已在描述符filedes上打开的文件的有关信息
lstat()返回该符号链接的有关信息,而不是有该符号链接引用的文件信息
利用stat实现ls的基本功能:
#include  #include  #include  #include  #include  #include  #include  int main(int argc, char **argv)
{ struct stat st; int i; struct passwd *pw; struct group *gr; struct tm *tm;
    stat(argv[1], &st); switch(st.st_mode & S_IFMT) //判断是何种类型的文件  { case S_IFREG:  printf("-"); break; case S_IFDIR:  printf("d"); break; case S_IFLNK:  printf("l"); break; case S_IFBLK:  printf("b"); break; case S_IFCHR:  printf("c"); break; case S_IFIFO:  printf("p"); break; case S_IFSOCK: printf("s"); break;
    } for(i = 8; i >= 0; i--)
    { if(st.st_mode & (1 << i)) //st.st_mode后8位,判断文件权限  { switch(i%3)
            { case 2: printf("r"); break; case 1: printf("w"); break; case 0: printf("x"); break;
            }
        } else printf("-");
    }

    pw = getpwuid(st.st_uid);
    gr = getgrgid(st.st_gid);


    printf("%2d %s %s %4ld", st.st_nlink, pw->pw_name, gr->gr_name, st.st_size);


    tm = localtime(&st.st_ctime);
    printf(" %04d-%02d-%02d %02d:%02d",tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);

    printf(" %s\n", argv[1]); return 0;
}
阅读(991) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~