7.标准文件库

1  查询文件属性

<sys/types.h>

<sys/stat.h>

int stat(const char *filename ,struct stat *buf)  获取文件属性

int fstat(int fd,struct stat *buf)  功能同stat,但是入参是文件描述符

int Istat(const char *filename,struct stat *buf)  功能同stat,但是嫩处理连接文件

查询文件属性

struct stat

dev_t st_dev //设备ID

mode_t st_mode //文件类型与权限(ls显示的 –rwx------)

nlink_t st_nlink //文件链接数

uid_t st_uid //文件所有人 ID

gid_t st_gid //文件所属组 ID

off_t st_size //文件大小

time_t st_atime //最近访问时间(access)

time_t st_mtime //最近修改时间(modify)

time_t st_ctime //文件状态最近改变时间(change)

查询文件属性

文件类型

S_IFREG 普通文件 -

S_IFDIR 目录文件 d

S_IFCHR 字符文件 c

S_IFBLK 块文件 b

S_IFIFO 管道文件 p

S_IFLNK 符号链接 l

S_IFSOCK 套接字文件 s

 

S_IFMT       掩码

判断方法: if (st_mode & S_IFMT)

 

查询文件属性

宏判断, 返回 0或者1

S_ISREG(st_mode) /* 判断是否为普通文件 - */

S_ISDIR(st_mode) /* 判断是否为目录文件 d */

S_ISCHR(st_mode) /* 判断是否为字符文件 c */

S_ISBLK(st_mode) /* 判断是否为块文件 b */

S_ISFIFO(st_mode) /* 判断是否为管道文件 p */

S_ISLNK(st_mode) /* 判断是否为符号链接 l */

S_ISSOCK(st_mode) /*判断是否位套接字文件*/

判断文件类型:

 1 #include <stdio.h>
 2 #include <sys/stat.h>
 3 
 4 int main(int argc,char **argv)
 5 {
 6     if(argc == 1)
 7     {
 8         return 0;
 9     }
10     
11     struct stat stInfo;
12     
13     int ret = stat(argv[1],&stInfo);
14     
15     if(ret!=0)
16     {
17         perror("Fail to stat");
18         return ret;
19     }
20     
21     switch(stInfo.st_mode & S_IFMT)
22     {
23         case S_IFREG:
24             printf("Regulat file
");
25             break;
26         case S_IFDIR:
27             printf("Directory file
");
28             break;
29         case S_IFCHR:
30             printf("Charactor file
");
31             break;
32         case S_IFBLK:
33             printf("BLock file
");
34             break;
35         case S_IFIFO:
36             printf("FIFO file
");
37             break;
38         case S_IFLNK:
39             printf("link file
");
40             break;
41         case S_IFSOCK:
42             printf("Sock file
");
43             break;
44         default:
45             printf("Unknow file
");
46             break;
47     }
48     
49     return 0;
50 }
#include <stdio.h>
#include <sys/stat.h>

int main(int argc,char **argv)
{
    if(argc == 1)
    {
        return 0;
    }
    
    struct stat stInfo;
    
    int ret = stat(argv[1],&stInfo);
    
    if(ret!=0)
    {
        perror("Fail to stat");
        return ret;
    }
    
    //获得文件类型
    switch(stInfo.st_mode & S_IFMT)
    {
        case S_IFREG:
            printf("Regulat file
");
            break;
        case S_IFDIR:
            printf("Directory file
");
            break;
        case S_IFCHR:
            printf("Charactor file
");
            break;
        case S_IFBLK:
            printf("BLock file
");
            break;
        case S_IFIFO:
            printf("FIFO file
");
            break;
        case S_IFLNK:
            printf("link file
");
            break;
        case S_IFSOCK:
            printf("Sock file
");
            break;
        default:
            printf("Unknow file
");
            break;
    }
    
    //获得权限
    int authMasks[] = {0400,0200,0100,0040,0020,0010,0004,0002,0001};
    char authChars[] = {'r','w','x'};
    int i;
    for(i=0;i<sizeof(authMasks)/sizeof(authMasks[0]);i++)
    {
        if(stInfo.st_mode & authMasks[i])
        {
            printf("%c",authChars[i%3]);
        }
        else
        {
            printf("-");
        }
    }
    printf("
");
    
    return 0;
}

 

2  查询文件访问属性

S_IRUSR  S_IWUSR   S_IXUSR

S_IRGRP     S_IWGRP     S_IXGRP

S_IROTH   S_IWOTH   S_IXOTH

if (st_mode & S_IRUSR)

r w x usr grp oth 权限和使用者

事实上,这些宏,只是把 000 000 000 这个 9 个bit位的数字,对应位置置位位1 (高位补0)。

S_IRUSR = 0400   S_IWUSR = 0200   S_IXUSR =0100

S_IRGRP = 0040

 3  文件操作

文件操作

#include<stdio.h>

FILE* fopen(const char* filename, const char* mode) 打开文件

FILE* freopen(const char* filename, const char* mode, FILE* stream) 文件重定向, 操作 stream文件时候,实际操作的是 filename文件

int fclose(FILE* stream) 关闭文件

int remove(const char *filename) 删除文件

int rename(const char *oldname, const char * newname) 重命名文件

文件重定向

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 //文件重定向
 6 void Test_freopen()
 7 {
 8     FILE *pFile;
 9     char szBuf[100];
10     //把stdout重定向到1.txt,把输入到内容输出到1.txt
11     /*
12     fprintf(stdout,"this is line1
");
13     
14     //文件重定向到1.txt
15     pFile = freopen("1.txt","w",stdout);
16     if(pFile == NULL)
17     {
18             perror("Fail to open");
19             return;
20     }
21     fprintf(stdout,"This is lin2
");
22     
23     printf("This is line3
");
24     
25     fprintf(pFile,"This is line for pFile
");
26     
27     fprintf(stdout,"This is end line
");
28     
29     */
30     
31     //把stdin重定向到1.txt从1.txt中获得
32     pFile = freopen("1.txt","r",stdin);
33     if(pFile == NULL)
34     {
35             perror("Fail to open");
36             return;
37     }
38     fgets(szBuf,100,pFile);
39     fprintf(stdout,"Your input is %s
",szBuf);
40     fclose(pFile);
41     
42     return;
43 }
44 
45 int main()
46 {
47     Test_freopen();
48     return 0;
49 }

标准输入

int getc(FILE *stream)

从文件流中读取字符,宏定义方式,效率高,括号里面不要进行运算

 

int fgetc(FILE *stream)

从文件中读取字符,函数定义形式,效率比getc()低

 

int getchar(void)

从stdin读入一个字符, 事实上,就是 getc(stdin)

 

char *gets(char *s)

从stdin 中读取字符存入 s,返回NULL或者s地址

 

char *fgets(char *s, int n, FILE *stream)

加入流长度控制

 

标准输入

注意返回值是 int,不是 char

返回 EOF = int(-1) = 0xffffffff 时表示读取结束

读文件

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 void ReadFile(char *szFile)
 6 {
 7     FILE *pFile = fopen(szFile,"r");
 8     if(pFile == NULL)
 9     {
10         perror("Fail to open");
11         return;
12     }
13     int c;
14     while((c=fgetc(pFile))!=EOF)
15     {
16         printf("%c",(char)c);
17     }
18     fclose(pFile);
19     
20     return;
21 }
22 
23 int main(int argv,char *args[])
24 {
25     if(argv==2)
26     {
27         //读文件
28         ReadFile(args[1]);
29     }
30     else
31     {
32         printf("open error");
33     }
34     return 0;
35 }

 二进制文件读写

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 
 6 typedef struct tagAAA
 7 {
 8     int x;
 9     int y;
10     char szBuf[8];
11 }AAA;
12 
13 void TestRw()
14 {
15     FILE *pFile;
16     AAA arrA[] = {{1,2,"Hello",},{3,4,"world"}};
17     
18     pFile = fopen("aaa","w");
19     
20     if(pFile == NULL)
21     {
22         perror("fail to fopen");
23         return;
24     }
25     //写入二进制数据
26     fwrite(arrA,sizeof(AAA),2,pFile);
27     fclose(pFile);
28     
29     return;    
30 }
31 
32 void Test_fread()
33 {
34         FILE *pFile;
35         AAA A;
36         
37         pFile = fopen("aaa","r");
38         if(pFile == NULL)
39         {
40             perror("Fail to fopen");
41             return;
42         }
43         
44         fread(&A,sizeof(AAA),1,pFile);
45         printf("{%d,%d,%s}
",A.x,A.y,A.szBuf);
46         
47         fread(&A,sizeof(AAA),1,pFile);
48         printf("{%d,%d,%s}
",A.x,A.y,A.szBuf);
49         
50         fclose(pFile);
51         
52         return;
53 }
54 
55 int main(int argv,char *args[])
56 {
57     //TestRw();
58     Test_fread();
59     return 0;
60 }

 可变参数实现写日志功能

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <unistd.h>
  5 #include <stdarg.h>
  6 
  7 
  8 typedef struct tagAAA
  9 {
 10     int x;
 11     int y;
 12     char szBuf[8];
 13 }AAA;
 14 
 15 void TestRw()
 16 {
 17     FILE *pFile;
 18     AAA arrA[] = {{1,2,"Hello",},{3,4,"world"}};
 19     
 20     pFile = fopen("aaa","w");
 21     
 22     if(pFile == NULL)
 23     {
 24         perror("fail to fopen");
 25         return;
 26     }
 27     //写入二进制数据
 28     fwrite(arrA,sizeof(AAA),2,pFile);
 29     fclose(pFile);
 30     
 31     return;    
 32 }
 33 
 34 void Test_fread()
 35 {
 36         FILE *pFile;
 37         AAA A;
 38         
 39         pFile = fopen("aaa","r");
 40         if(pFile == NULL)
 41         {
 42             perror("Fail to fopen");
 43             return;
 44         }
 45         
 46         fseek(pFile,sizeof(AAA),SEEK_SET);
 47         fread(&A,sizeof(AAA),1,pFile);
 48         printf("{%d,%d,%s}
",A.x,A.y,A.szBuf);
 49         
 50         rewind(pFile);
 51         fread(&A,sizeof(AAA),1,pFile);
 52         printf("{%d,%d,%s}
",A.x,A.y,A.szBuf);
 53         
 54         fclose(pFile);
 55         
 56         return;
 57 }
 58 
 59 void TestPrint()
 60 {
 61         //fclose(stdout);
 62         printf("1");
 63         fprintf(stderr,"2");
 64         fprintf(stdout,"3");
 65         fprintf(stderr,"4");
 66         
 67         char szBuf[100];
 68         scanf("%s",szBuf);
 69         printf("OK
");
 70         
 71         getchar();
 72         fclose(stdin);
 73         while(1);
 74         return;
 75 }
 76 
 77 //不定参数求和
 78 int sum(int start,...)
 79 {
 80         int iRet = 0;
 81         int num = 0;
 82         
 83         
 84         va_list args;
 85         //初始化args,args指向start后面的参数
 86         va_start(args,start);
 87         
 88         for(int i=0;i<start;i++)
 89         {
 90             num = (int)va_arg(args,int);
 91             iRet += num;
 92         }
 93         
 94         va_end(args);
 95         
 96         return iRet;
 97 }
 98 
 99 void WriteLog(char *fmt,...)
100 {
101     FILE *pFile;
102     pFile = fopen("log.log","a");
103     if(pFile  == NULL)
104     {
105         perror("Fail to fopen");
106         return;
107     }
108     
109     va_list args;
110     va_start(args,fmt);
111     
112     char szBuf[1024]={};
113     
114     vsnprintf(szBuf,1024,fmt,args);
115     
116     fprintf(pFile,"%s",szBuf);
117     
118     fclose(pFile);
119     va_end(args);
120     
121     return;
122     
123 }
124  
125 
126 int main(int argv,char *args[])
127 {
128     //TestRw();
129     //Test_fread();
130     //TestPrint();
131     printf("%d
",sum(1,2));
132     printf("%d
",sum(2,2,3));
133     
134     WriteLog("This is log %d
",1);
135     WriteLog("Fatal error[%d]:%s
",EOF,"File not found");
136     return 0;
137 }
原文地址:https://www.cnblogs.com/xiaochi/p/8977490.html