Linux系统文件编程(1)

打开文件

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

open----返回的是文件描述符是整形数(文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。在程序设计中,一些涉及底层的程序编写往往会围绕着文件描述符展开。但是文件描述符这一概念往往只适用于UNIX、Linux这样的操作系统。)打开成功返回非负数,失败返回-1。
pathname----要打开的文件名称。
flags-----是打开文件的权限包含
{
O_RDONLY——只读打开
O_WRONLY——只写打开
O_RDWR——可读可写打开(常用)
}
示例如下

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
int main()
{
        int fd;
        fd=open("./file1",O_RDWR);//file1是要打开的文件,fd是文件描述符
        printf("fd=%d\n",fd);
        return 0;
}
ps:头文件可用man手册查看

Mode---- 一定是在flags中使用了O_CREAT标志,mode记录待创建文件的访问权限。
权限:
{
可读 r 4
可写 w 2
可执行 x 1
}
例如:0600 表示可读可写(4+2=6)
文件不存在则打开失败要用到O_CREAT
示例如下

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
int main()
{
        int fd;
        fd=open("./file1",O_RDWR);
        if(fd==-1){
                printf("open file1 fail");
             	fd=open("./file1",O_RDWR|O_CREAT,0600);//文件不存在则创建文件,0600是待创建文件的权限。
                if(fd>0){
                        printf("creat file1 fd=%d\n",fd);
                }
         }
        return 0;
}

写入文件

ssize_t write(int fd, const void *buf, size_t count);
第一个参数是文件描述符
第二个参数是缓冲区(就是要写入内容的指针)
第三个参数是要写入文件的大小
若写入成功返回写入的字节数,失败则返回-1.

代码示例

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>
int main()
{
        int fd;
        char*buf="i am handsome";//将要写入文件的内容
        fd=open("./file1",O_RDWR);//"./file1"    is    wenjianlujing
        if(fd==-1){
                printf("open file1 fail\n");
                fd=open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("creat file1\n");
                }
        }
        printf("open successful  fd=%d\n",fd);
        write(fd,buf,strlen(buf));//strlen计算字符串的长度,sizeof()在这里计算的是指针的大小
        close(fd);//写完文件后要关闭文件
        return 0;

}

文件的读取

ssize_t read(int fd, void *buf, size_t count);
fd:文件描述符
buf:把读取的数据放到buf中
count:所要读取的字节数
若成功则返回读取的字节数,失败返回-1.

代码示例

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
        int fd;
        char*buf="i am handsome";//将要写入文件的内容
        fd=open("./file1",O_RDWR);//"./file1"是文件路径
        if(fd==-1){
                printf("open file1 fail\n");
                fd=open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("creat file1\n");
                }
        }
        printf("open successful  fd=%d\n",fd);
        int n_write=write(fd,buf,strlen(buf));//返回字节的长度
        if(n_write!=-1){
                printf("write successfuli %d\n",n_write);
        }
        close(fd); 重新打开定位光标(光标在文件的头)
        fd=open("./file1",O_RDWR);
        char*readbuf;
        readbuf=(char *)malloc(n_write*sizeof(char));
        int n_read=read(fd,readbuf,n_write);
        printf("read %d,context %s\n",n_read,readbuf);
        close(fd);
        return 0;
}

文件光标的移动操作

off_t lseek(int fd, off_t offset, int whence);
fd:文件描述符
offset:将读写指针相对whence移动offset个字节,负数向前偏移,正数向后偏移
whence:
{	SEEK_SET:文件的头
	SEEK_CUR:当前位置
	SEEK_END:文件的尾
}
调用成功返回从当前位置到文件头所偏移的字节数,失败则返回-1.

代码改进

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
        int fd;
        char*buf="i am handsome";//将要写入文件的内容
        fd=open("./file1",O_RDWR);
        if(fd==-1){
                printf("open file1 fail\n");
                fd=open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                        printf("creat file1\n");
                }
        }
        printf("open successful  fd=%d\n",fd);
        int n_write=write(fd,buf,strlen(buf));
        if(n_write!=-1){
                printf("write successfuli %d\n",n_write);
        }
//      close(fd);       
//      fd=open("./file1",O_RDWR);
        lseek(fd,-n_write,SEEK_END);
        char*readbuf;
        readbuf=(char *)malloc(n_write*sizeof(char));
        int n_read=read(fd,readbuf,n_write);
        printf("read %d,context %s\n",n_read,readbuf);
        close(fd);
        return 0;
}

可利用lseek计算文件的字节数

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>
 #include <stdlib.h>
int main()
{
        int fd;
        char*buf="i am handsome";
        fd=open("./file1",O_RDWR);
        int filesize=lseek(fd,0,SEEK_END);
        printf("size=%d\n",filesize);
        close(fd);
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值