Linux文件I/O编程

Linux编程实验——Linux文件I/O编程

背景知识

● open()函数用于打开或创建文件,在打开或者创建文件时可以指定文件的属性及用户的权限等各种参数。

● close()函数用于关闭一个被打开的文件。当一个进程终止时,所有被它打开的文件都由内核自动关闭,很多程序都使用这一功能而不显示地关闭一个文件。

● read()函数用于将从指定的文件中读出的数据放到缓存区中,并返回实际读入的字节数。若返回0,则表示没有数据可读,即已到达文件尾。读操作从文件的当前指针位置开始。当从设备文件中读出数据时,通常一次最多读一行。

● write()函数用于向打开的文件写数据。写操作从文件的当前指针位置开始,对磁盘文件进行写操作,若磁盘已满或者超出该文件的长度,则write()函数返回失败。

● lseek()函数用于在指定的文件描述符中将文件指针定位到相应的位置。每一个已打开的文件都有一个读写位置,当打开文件时,其读写位置通常指向文件开头;若是以附加的方式打开文件(如O_APPEND),则读写位置会指向文件尾。当read()或者write()时,读写位置会随之增加,lseek()便是用来控制该文件的读写位置的。它只能用在可定位(可随机访问)文件操作中。管道、套接字和大部分字符设备文件是不可定位的,所以在这些文件的操作中无法使用lseek()调用。

向data.txt写入字符

       编写一个程序 mywrite.c ,向当前目录的data.txt文件中写入“Hello,Linux!,I can write a file.”字符串。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[]) {

    int fd = -1; //文件描述符
     //打开文件
    fd = open( "data.txt", O_RDWR );

    if ( -1 == fd ) {
        printf("文件打开失败\n");
    }else {
        printf("文件打开成功,fd=%d\n", fd );
    }
     //写文件
    char buf[] = "Hello,Linux!,I can write a file.";
    int count = 0;
    count = write( fd, buf, strlen( buf ) );
    if ( -1 == count ) {
        printf("文件写入失败\n");
    }else {
        printf("文件写入成功,实际写入的字节数目为:%d\n", count);
    }

    //关闭文件
    close( fd );

    return 0;
}          

从data.txt读取字符

       编写一个程序 myread.c,从当前目录的data.txt 文件中读取数据,将读取的数据显示出来。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char const *argv[]) {

     int fd = -1; //文件描述符
    //打开文件
    fd = open( "data.txt", O_RDWR );

    if ( -1 == fd ) {
        printf("文件打开失败\n");
    }else {
        printf("文件打开成功,fd=%d\n", fd );
    }

    //读取文件
    int count = 0;
    char buf[20];
    count = read( fd, buf, 50 );
    if ( -1 == count ) {
        printf("文件读取失败\n");
    }else {
        printf("文件读取成功,实际读取的字节数目为:%d\n内容为%s\n", count, buf );
     }

    //关闭文件
    close( fd );

    return 0;
}

复制文件操作

       编写一个程序 mycopy.c,将/usr/bin/info 文件复制到当前目录下,名称为myinfo。复制后,赋予myinfo执行权限,看看是否能够执行。

#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
int main()
{
    const char* pathName="myinfo.c";
    int in,out,flag;
    char buffer[10240];
    in=open("//usr//bin//info",O_RDONLY,S_IRUSR);
    if(in==-1)
    {
        printf(" 打开文件info失败 !\n");
        return -1;
    }
    out=creat(pathName,S_IWUSR);
    if(in==-1)
    {
        printf("创建文件myinfo失败!\n");
        return -1;
    }
    while((flag=read(in,buffer,10240))>0)
    {
        write(out,buffer,flag);
    }
    close(in);
    close(out);
    printf("复制文件info到myinfo完成!\n");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值