标准I/O常用函数以及Linux文件夹操作函数

来源:互联网 发布:字体转换软件下载 编辑:程序博客网 时间:2024/06/05 04:28

标准I/O(#include<stdio.h>)

1.        FILE*  fopen(const char *filename,const char *mode);

         参数:

                   filename:文件名;

                   mode:模式:

                           

文本文件

二进制文件

意义

r

rb

只读

w

wb

只写

a

ab

追加

r+

rb+

修改方式打开

w+

wb+

修改方式打开,并把文件长度置为零

a+

ab+

修改方式打开,在文件后追加

2.        读写

size_t  fread(void *ptr, size_t  size, size_t nitem,  FILE*  stream);

size_t  fwrite(void *ptr, size_t  size, size_t nitem,  FILE*  stream);

参数:

         ptr:将要数据存放的字符串

         size:没错读取长度

nitem:操作次数

stream:文件指针

3.        关闭文件

int  flose(FILE* stream);

4.        将文件流中未写入的数据立刻写入文件中

int fflush(FILE  *stream);

5.        I/O中的lseek

int fseek(FILE  *stream, long intoffset, int whence);

参数:

stream:文件指针;

         offset: 要设置文件指针要的位置;

         whence:定义偏移值的用法,可取下面值:

SEEK_SET

offset是一个绝对文章

SEEK_CUR

offset是一个相对于当前的相对位置

SEEK_END

offset是一个相对于文件尾的相对位置

6.        单个获取、输入字符

int fgetc(FILE *steam);

int getc(FILE *steam);

int getchar();

int fputc(FILE *steam);

int putc(FILE *steam);

int putchar();

7.        获取字符串函数

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

char *gets(char *s);

返回指向s的指针。

8.        格式化输入输出

int fprintf(FILE *stream, const char *format….);

int printf(const  char *format….);

int sprint(char *s, const  char*format….);

 

int scanf(const  char *format….);

int fscanf(FILE *stream, const char *format….);

int sscanf(char *s, const  char*format….);

9.        文件流错误

#include <errno.h>

int ferror(FILE *stream);

int feof(FILE *stream);

void clearer(FILE *stream);

10.    字符串拷贝

#include<string.h>

void*memcpy(void *dest, const void *src, size_t n);

函数返回dest的值。

目录操作(#include <sys/types.h> #include <dirent.h>)

文件夹结构体:

struct dirent{

   ino_t d_ino; //d_ino 此目录进入点的inode

   ff_t d_off; //d_off 目录文件开头至此目录进入点的位移

   signed short int d_reclen; //d_reclen _name 的长度, 不包含NULL 字符

   unsigned char d_type; //d_type d_name 所指的文件类型 d_name 文件名

   char d_name[256];  文件名

};

此部分,可参看文件:“文件夹操作.docx”

1.        打开、读、关闭目录

DIR*opendir(const char *name);

structdirent *readdir(DIR *dirp);

intclosedir(DIR *dirp);

2.        返回当前位置

long int telldir(DIR *dirp);

3.        设置文件夹指针

Void seekdir(DIR *dirp, long int loc);

4.        范例

#include<sys/types.h>

#include <dirent.h>

#include <unistd.h>

main()

{

    DIR * dir;

    struct dirent * ptr;

    int i;

    dir = opendir("/etc/rc.d");

    while((ptr = readdir(dir)) != NULL)

    {

        printf("d_name : %s\n",ptr->d_name);

    }

    closedir(dir);

}

5.        获得文件夹详细信息

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

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

int fstat(int fd, struct stat*buf);

intlstat(const char *path, struct stat *buf); 路径为链接时,获取链接本身的信息,而非链接指向的文件的信息。

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 file system I/O */

               blkcnt_t st_blocks;  /* number of 512Bblocks 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 */

};

附:判断文件类型:

Structstat statbuf;

S_ISREG(statbuf.mode)  : is it a regular file?

S_ISDIR(statbuf.mode)  : directory?

S_ISCHR(statbuf.mode)  : character device?

S_ISBLK(statbuf.mode)  :block device?

S_ISFIFO(statbuf.mode) FIFO : (nastatbuf.modeedpipe)?

S_ISLNK(statbuf.mode)  : systatbuf.modebolic link? (Not inPOSIX.1-1996.)

S_ISSOCK(statbuf.mode) : socket?(Not in POSIX.1-1996.)

原创粉丝点击