【文件】总结(一)

来源:互联网 发布:iphone mac连接 编辑:程序博客网 时间:2024/06/05 05:27

1. creat

函数的作用:创建一个文件;

函数的原型:int creat(const char*pathname, mode_t mode);

文件头:#include<sys/types.h>

            #include <sys/stat.h>

             #include <fcntl.h>

返回值:成功——新的文件描述符

            出错——-1

示例:

#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> void  create_file(char *filename){     if(creat(filename,0755) < 0)    {         printf("create file %s failure!\n",filename);         exit(EXIT_FAILURE);     }    else    {         printf("create file %s success!\n",filename);     } } int main(int argc, char *argv[]){     int i;         if(argc < 2)    {         perror("you haven't input the filename,please try again!\n");         exit(EXIT_FAILURE);     }     for(i = 1; i < argc; i++)    {         create_file(argv[i]);        }     exit(EXIT_SUCCESS); }

        

2. open

函数的作用:打开一个文件;

函数的原型:int open(const char *pahtname, int flags);

                    int open(const char *pahtname, int flags, mode_tmode);

返回值:文件描述符——成功;

             出错——-1

flags: 参数

    O_RDONLY:只读

    O_WRONLY:只写

    O_RDWR:可读写

          O_CREAT:如果原来折耳根文件不存在,那么有这个参数就可以创建这个文件;

          O_APPEND:原来有内容,则会自动保留文件内容,自动向下读写;

         O_TRUNC:文件存在,有内容,文件清空;

示例:

#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(int argc, char *argv[]){    int fd;        if(argc < 2)    {        puts("please input the open file pathname!\n");        exit(1);    }        //如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755    //如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错.    //所以fd = open(argv[1],O_RDWR),仅仅只是打开指定文件    if((fd = open(argv[1], O_CREAT | O_RDWR, 0755)) < 0)    {        perror("open file failure!\n");        exit(1);    }    else    {        printf("open file %s success!\n",argv[1]);        printf("return %d.\n",fd);    }    close(fd);    exit(0);   }



3. read

函数的作用:从打开的文件中读取数据

函数的原型:ssize_t read(int fd, void *buf, size_t count);

头文件:#include <unistd.h>

返回值:正常是实际读到的字节数;

           如果是在文件结束或者是无数据,返回0

           出错,-1

 

4. write

函数的作用:向打开的文件中写数据

函数的原型:ssize_t write(int fd, const void *buf, size_t count);

头文件:#include<unistd.h>

返回值:成功会返回实际写入的字节数;

            出错:-1

示例:

#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define MAX_SIZE 1024int main(){    int fd;    int len;    char str[MAX_SIZE];    fd = open("hello.txt",O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);    if(fd > 0)    {        write(fd,"Hello world.",strlen("Hello world."));        close(fd);    }    fd = open("hello.txt",O_RDWR);    //lseek(fd,0,SEEK_SET);    len = read(fd,str,100);    str[len] = '\0';    printf("%s\n",str);    printf("the length is %d.\n",len);    close(fd);}

        

5. lseek

函数的功能:进行文件定位

函数的原型:int lseek(int fd, offset_toffset, int whence);

函数的参数:offset: 指针的微调,在指定的指针向前移动为负,向后为正;

                   whenceSEEK_SET:放在文件头

                                  SEEK_CUR:当前的位置;

                                  SEEK_END 文件尾;

返回值:返回文件当前指针到文件开始的地方有多少字节;

          出错-1

· 读取一个文件的大小:

lseek(fd,0,SEEK_END);
             

6. fopen

函数的作用: 打开文件

函数的原型: FILE *fopen(constchar *pth, const char *mode)

mode:

  r:读,文件必须存在;

  r+:打开可读写,文件必须存在;

  w:打开只写文件,文件不存在就会创建文件;文件清0

  w+:打开可读写的文件,

  a:附加的形式打开只写文件,不存在就创建,存在就写到原来的文件尾。

  a+:以附加的形式打开可读写的文件,不存在就创建,存在就写到原来的文件尾。

  b:二进制文件

文件头:#include <stdio.h>

返回值:成功是指向=文件流的指针;

            出错返回NULL    

 

7. fputc

函数的作用:将一个指定的字符写入到文件流中;

函数的原型:int fputc(int c, FILE*stream);

返回值:返回写入成功的字符,c 

             EOF则表示失败。

 

8. fgetc

函数的作用:从文件流中读取一个字符

函数原型:int fgetc(FILE *stream);

返回值:返回值正常的是读取的字符;

             EOF表示到了文件尾;

 

9. fputs

函数的作用:将一个字符串写入到文件内

函数的原型:int fputs(const char *s, FILE *stream)

返回值:成功返回写成字符数;

             EOF表示出错

 

10. fgets

函数的作用:从文件中读取一个字符串;

函数的原型:char *fgets(char *s, int size, FILE *stream);

 返回值:成功返回s, 出错NULL

 

11. fread

函数的作用:从文件流中读取数据块

函数原型:size_t fread(void *ptr, size_t size,size_t nmemb, FILE * stream);

返回值:返回实际读到数据块的数目

             nmember小的话,可能是到了文件尾,或者错误发生。

             feof:到文件尾

              ferror:判断错误的


12. fwrite

函数的作用:将数据块写到文件流中:

函数原型:size_t fwrite(const void * ptr, size_tsize, size_t nmemb, FILE *stream);

返回值:实际写入的nmemb数目;

 

13.fseek

函数的作用:移动文件流的读写位置

函数的原型:int fseek(FILE *stream, long offset, int whence)

返回值:成功返回0, 出错-1


14. ftell

函数的作用:读取文件流的读写位置;

函数的原型:long ftell(FILE *stream)

返回值: 成功返回当前的读写位置;

             出错-1

        

15. feof

函数的作用:检测文件流是否到了文件尾

函数的原型:int feof(FILE *steam)

返回值: 非零代表到了文件尾,其他是0

 

16. fprintf

函数的作用:格式化数据到文件

函数的原型:int fprintf(FILE*stream, const char *format, ....);

返回值:成功返回实际输入的字符数,失败-1


17. fscanf

函数的作用:格式化字符串输入

函数的原型:int fscanf(FILE*strem, const char *fromat,....)

返回值:成功返回参数数目,出错-1


关于文件的复制的两种方法:


0 0
原创粉丝点击