Linux编程文件

1,unlink的临时文件

#include <sys/stat.h>                                                               
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main(void)
{
     int fp = open("tempfile", O_CREAT | O_RDWR,0664);

      if (fp == -1)
     {
         perror("open");
         exit(1);
     }

    //删除临时文件file
     int ret = unlink("tempfile");

     // write file
     write(fp, "hello\n", 5);

    //重置文件指针
    lseek(fp, 0, SEEK_SET);   
    // read file 
    char buf[24] = { 0 };
    int len = read(fp, buf, sizeof(buf));

    //将读出的内容 ,屏幕上
    write(1, buf, len );

   //close file
    close(fp);

     return 0;
 }                                                                                   

unlink文档说明

UNLINK(2)                     Linux Programmer's Manual                     UNLINK(2)

NAME
       unlink, unlinkat - delete a name and possibly the file it refers to

SYNOPSIS
       #include <unistd.h>

       int unlink(const char *pathname);

       #include <fcntl.h>           /* Definition of AT_* constants */
       #include <unistd.h>

       int unlinkat(int dirfd, const char *pathname, int flags);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       unlinkat():
           Since glibc 2.10:
               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:
               _ATFILE_SOURCE

DESCRIPTION
       unlink()  deletes  a name from the filesystem.  If that name was the last link
 Manual page unlink(2) line 1 (press h for help or q to quit)

2,文件重命名 rename 函数

文档说明


NAME
       rename, renameat, renameat2 - change the name or location of a file

SYNOPSIS
       #include <stdio.h>

       int rename(const char *oldpath, const char *newpath);

       #include <fcntl.h>           /* Definition of AT_* constants */
       #include <stdio.h>

       int renameat(int olddirfd, const char *oldpath,
                    int newdirfd, const char *newpath);

       int renameat2(int olddirfd, const char *oldpath,
                     int newdirfd, const char *newpath, unsigned int flags);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       renameat():
           Since glibc 2.10:
               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:
 Manual page rename(2) line 1 (press h for help or q to quit)

3,目录操作的函数

* 1,chdir 函数 *
修改当前进程的路径:函数原型: int chdir(const char *path);

* 2,getcwd 函数*
获取当前进程工作目录:函数原型: char *getcwd(char *buf, size_t size);

* 3, mkdir 函数*
int mkdir(const char *pathname, mode_t mode);

   #include <fcntl.h>           /* Definition of AT_* constants */
   #include <sys/stat.h>

   int mkdirat(int dirfd, const char *pathname, mode_t mode);

* 4, rmdir 函数*
删除一个空目录

   int rmdir(const char *pathname);

DESCRIPTION
rmdir() deletes a directory, which must be empty.

* 5, opendir 函数*
打开目录

S
       #include <sys/types.h>
       #include <dirent.h>

       DIR *opendir(const char *name);
       DIR *fdopendir(int fd);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       fdopendir():
           Since glibc 2.10:
               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
           Before glibc 2.10:
               _GNU_SOURCE

* 6, readdir函数*

   readdir, readdir_r - read a directory

SYNOPSIS
       #include <dirent.h>

       struct dirent *readdir(DIR *dirp);

       int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       readdir_r():
           _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE ||
           _POSIX_SOURCE

SCRIPTION
       The readdir() function returns a pointer to a  dirent  structure  representing
       the  next  directory  entry  in  the  directory stream pointed to by dirp.  It
       returns NULL on reaching the end of  the  directory  stream  or  if  an  error
       occurred.

       On Linux, the dirent structure is defined as follows:

           struct dirent {
               ino_t          d_ino;       /* inode number */
               off_t          d_off;       /* not an offset; see NOTES */
               unsigned short d_reclen;    /* length of this record */
               unsigned char  d_type;      /* type of file; not supported
                                              by all filesystem types */
               char           d_name[256]; /* filename */
           };



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值