使用文件描述符

打开和关闭文件描述符。

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

int open(const char *pathname, int flags);

int close(int fd);

Open试图打开参数pathname中的一个文件。
参数flags指定访问该文件的方式。
必须把flags设置为O_RDONLY、O_WRONLY、O_RDWR、O_CREAT、O_APPEND分别表示只读、只写、读写、如果文件不存在就创建、追加。
Open成功后会返回一个文件描述符。
Open失败后会返回-1,并设置errno变量。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
        char s[] = "abc.txt";
        int fd = open(s, O_RDONLY);
        printf("fd=%d
", fd);
        return 0;
}
Open试图打开一个不存在的文件,返回-1。
如果想知道更加详细的错误描述,请使用errno和strerror函数
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int main()

{

  char s[] = "abc.txt";

  int fd = open(s, O_RDONLY);

  if (fd == -1)

  {

  printf("error is %s ", strerror(errno)); 

  }

  else

  {

  printf("success fd=%d ", fd);

  }

  return 0;

}

在一个文件描述符用完后一定要用close()函数关闭它,这样才能保证该进程对文件所有加的锁全部被释放。
 
读写文件描述符。

ssize_t read(int fd, void *buf, size_t count);

ssize_t write(int fd, void *bug, size_t count);

read用于从文件描述符对应的文件读取数据,调用成功返回读出的字节数,
参数fd必须是用open调用返回的有效文件描述符。
参数buf为读出数据的缓冲区,count指定读出的字节数。
成功返回读取字节数,如果遇到EOF,返回0,出错返回-1。
 
 
read函数例子。
int main()    
{
    char s[] = "abc.txt";
    int fd = open(s, O_RDONLY);
    if (fd == -1)
    {
        printf("error is %s
", strerror(errno));        
    }
    else
    {
        printf("success fd=%d
", fd);
    char buf[100];
    memset(buf, 0, sizeof(buf));
    int i = read(fd, buf, sizeof(buf));
    printf("%s
", buf);
    close(fd);    
    }
    
    return 0;
}
read函数例子。
int main()    
{
    char s[] = "abc.txt";
    int fd = open(s, O_RDONLY);
    if (fd == -1)
    {
        printf("error is %s
", strerror(errno));        
    }
    else
    {
        printf("success fd=%d
", fd);
    char buf[100];
    memset(buf, 0, sizeof(buf));
    while (read(fd, buf, sizeof(buf)) > 0)
    {
        printf("%s", buf);
        memset(buf, 0, sizeof(buf));
    }
    close(fd);
    }    
    return 0;
}
write函数例子
int main()
{
    char s[] = "abc.txt";
    int fd = open(s, O_RDWR | O_APPEND);
    if (fd == -1)
    {
        printf("error is %s
", strerror(errno));        
    }
    else
    {
        printf("success fd=%d
", fd);
    char buf[100];
    memset(buf, 0, sizeof(buf));
    strcpy(buf, “hello world
");
    int i = write(fd, buf, strlen(buf));
    close(fd);    
    }
    return 0;
使用fstat获取文件信息。

int fstat(int fd, struct stat *buf)

参数fd必须是用open调用返回的有效文件描述符。
使用stat获取文件信息。

int stat(const char *path, struct stat *buf);

参数path是路径加文件名的字符串
Struct stat定义如下:
 
 struct stat {
              dev_t     st_dev;     /* ID of device containing file */
              ino_t     st_ino;     /* inode number */
              mode_t    st_mode;    /* protection */
              nlink_t   st_nlink;   /* number of hard links */
              uid_t     st_uid;     /* user ID of owner */
              gid_t     st_gid;     /* group ID of owner */
              dev_t     st_rdev;    /* device ID (if special file) */
              off_t     st_size;    /* total size, in bytes */
              blksize_t st_blksize; /* blocksize for filesystem I/O */
              blkcnt_t  st_blocks;  /* number of blocks allocated */
              time_t    st_atime;   /* time of last access */
              time_t    st_mtime;   /* time of last modification */
              time_t    st_ctime;   /* time of last status change */
          };
为了正确解释文件类型,有一套宏能够计算stat接口的st_mod成员。

              S_ISREG(m)  is it a regular file?

              S_ISDIR(m)  directory?

              S_ISCHR(m)  character device?

              S_ISBLK(m)  block device?

              S_ISFIFO(m) FIFO (named pipe)?

              S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)

              S_ISSOCK(m) socket? (Not in POSIX.1-1996.)

fstat函数例子。
 
int main()
{
    char s[] = "abc.txt";
    int fd = open(s, O_RDONLY);
    if (fd == -1)
    {
        printf("error is %s
", strerror(errno));
    }
    else
    {
        printf("success
");
    struct stat buf;
    fstat(fd, &buf);
    if (S_ISREG(buf.st_mode))
    {
        printf("%s is charfile
", s);
    }
    close(fd);
    }
    return 0;
}
stat函数例子。
int main()
{
        struct stat t;
        memset(&t, 0, sizeof(t));
        if (stat("a.txt", &t) == 0)
        {
                printf("%d
", t.st_size);
        }
        else
        {
                printf("open failed %s
", strerror(errno));
        }
        return 0;
}
读写用户输入,屏幕不回显

char *getpass( const char *prompt);

getpass用于从键盘读取用户输入,但屏幕不回显。
参数prompt为屏幕提示字符。
函数返回值为用户键盘输入的字符串。
 
getpass函数例子。
 

int main()

{

  char *s = getpass("please input:");

  printf("%s ", s);

  return 0;

}

 
原文地址:https://www.cnblogs.com/shichuan/p/4496327.html