linux文件编程

文件编程

open(打开文件/创建文件)write(文件写操作)read(文件读操作)lseek(光标)
fopen(打开文件/创建文件)fwrite(文件写操作)fread(文件读操作)fseek(光标)
O_APPEND(每次打开文本光标在末尾)O_EXCL(搭配o_creat,如果已经有文件,在创建会报错)O_TRUNC(打开文本会将原文本内容清零)fget
fputfeofstrstrcp

open打开文件,创建文件

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

brief:打开/创建文件都要用到上面这个函数

param:pathname,你需要创建/打开的文件路径,flag,创建或打开文件的权限有、O_RDWR、O_RDONLY、O_WRONLY,mode一般是在O_CREAT时待创建文件的访问权限

return:返回的是一个文件标识符,我们通过文件标识符来进行读写文件,返回 -1 表示没有该文件

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
​
​
void main()
{
​
        int fd;
​
         fd = open("./file1",O_RDWR);//如果有文件我就打开,O_RDONLY 只读打开、O_WRONLY 只写打开、O_RDWR 可读可写
​
         if(fd == -1)
         {
                printf("No file!\n");
​
                fd = open("./file1",O_RDWR|O_CREAT,0600);//O_CREAT 创建文件、O_EXCL 搭配O_CREAT 如果有该文件返回-1、O_APPEND 如果文件有                                                          //把光标定位在末尾、O_TRUNC 你打开的文件如果有内容就会被清空
                if(fd > 0)
                {
                        printf("create file succeed !\n");
                }
         }
​
}
输出结果:
No file!
create file succeed !

write写操作/read读操作

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

brief:读取文件操作

param :fd,文件标识符,通过文件标识符对文件读内容,buf,把fd读取出来的文件存在buf里面,cnt,读多少个字节

return:读取文件内容的字节个数

size_t write(int fd, const void *buf, size_t count);

brief:对文件写操作

param:fd,通过文件标识符对文件写内容,buf,把写的内容写给fd,cnt,要写的字节个数

return:写文件内容的字节个数 = -1,没读到数据

lseek光标操作

off_t lseek(int fd, off_t offset, int whence);

brief:对文件光标位置进行改变

param:fd,对fd这个文件光标进行改动,offest,光标偏移值,whecce,光标的模式,SEEK_SET,SEEK_END、SEEK_CUR

return:可以返回文件的大小

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
​
​
void main()
{
        int fd;
        char *buf = "Li jian hua";
        int w_write;
        int r_read;
        char *readbuf;
        int size;
        fd = open("./file2",O_RDWR);
        
        if(fd == -1)
        {
                printf("NO create file\n");
​
                fd = open("./file2",O_RDWR|O_CREAT,0600);
​
                if(fd > 0)
                {
                        printf("create file succeed!\n");
                }
        }
​
        w_write = write(fd,buf,strlen(buf));
        printf("write: %d\n",w_write);
        
        close(fd);
        fd = open("./file2",O_RDONLY);
        size = lseek(fd,0,SEEK_END);
        lseek(fd,0,SEEK_SET);
​
        readbuf = (char*)malloc(sizeof(char)*size);
​
        r_read = read(fd,readbuf,size);
        printf("read: %d\n",r_read);
        printf("readbuf:%s\n",readbuf);
​
        close(fd);
}
输出结果:
write: 11
read: 11
readbuf:Li jian hua

fopen打开/创建文件

FILE *fopen(const char *pathname, const char *mode);

brief:对文件打开和创建

param:pathname,打开或创建文件路径,mode,打开或创建文件模式,"r",只读,“w”,只写,“w+”,可读可写

return:返回的是一个文件指针

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
​
void main()
{
​
        FILE * fd;
​
        fd = fopen("./file","w+");
​
        if(fd == NULL)
        {
                printf("create error!");
        }
​
        printf("creat file succeed!\n");
}
输出结果:
create file succeed!

fread/fwrite文件读写操作

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

brier:读取文件内容

param:ptr,把读取到的内容存在ptr,size,读多少个字节,nmemb,读多少次,stream,文件指针

return:返回nmemb

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

brier:往文件写内容

param:ptr,把ptr的内容写到文件里面,size,写多少个字节,nmemb,写多少次,stream,文件指针

return:返回nmemb

fseek光标操作

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

brief:对文件光标位置进行改变

param:fd,对fd这个文件光标进行改动,offest,光标偏移值,whecce,光标的模式,SEEK_SET,SEEK_END、SEEK_CUR

return:可以返回文件的大小

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
​
void main()
{
​
        FILE * fd;
        char * buf = "Li jian hua!";
        char *readbuf;
        int size;
​
        fd = fopen("./file","w+");
​
        if(fd == NULL)
        {
                printf("create error!");
        }
​
        printf("creat file succeed!\n");
​
        fwrite(buf,sizeof(char)*strlen(buf),1,fd);
​
        fclose(fd);
​
        fd = fopen("./file","r");
        size = fseek(fd,0,SEEK_END);
        fseek(fd,0,SEEK_SET);
​
        readbuf = (char *)malloc(sizeof(char)*size);
        fread(readbuf,strlen(buf),1,fd);
​
        printf("readbuf: %s\n",readbuf);
​
        fclose(fd);
​
}
输出结果:
creat file succeed!
readbuf: Li jian hua!

fgetc/fputc文件读写操作

int fgetc(FILE *stream);

brief:读文件数据,一次性只能读取一个字符

param:stream,文件指针

return:int类型

int fputc(int c, FILE *stream);

brief:给文件写入数据,一次性只能写一个字符

param:stream,文件指针

return:int类型

int feof(FILE *stream);

brief:判断文件字符是否到达末尾

param:stream,文件指针

return:非0表示到达文件末尾,=0表示还未到达文件末尾

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
​
​
void main()
{
        FILE *fd;
        char *buf = "Li jian hua!";
        int len = strlen(buf);
        int i;
        char str;
​
        fd = fopen("./file","w+");
​
        for(i = 0;i < len;++i)
        {
                 fputc(*buf,fd);
                 buf++;
        }
​
        fclose(fd);
​
        fd = fopen("./file","r");
        printf("str:");
        while(!feof(fd))
        {
                str = fgetc(fd);
                printf("%c",str);
        }
​
        fclose(fd);
}
输出结果:
Li jian hua!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No Iverson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值