读文件系统函数,写文件系统函数

C语言文件读写实战

目录

第1关:读文件系统函数 

任务描述

相关知识

read函数

答案:

第1关:写文件系统函数

任务描述

相关知识

write函数

答案:


第1关:读文件系统函数 

任务描述

本关任务:当文件打开后,系统函数调用read()对文件进行读操作。首先在程序中打开文件testFile,然后将文件中前1024字节的内容读到缓冲区buffer中。

相关知识

为了完成本关任务,你需要掌握: 1.如何使用C语言读取文件;

read函数

read函数是系统调用从文件描述符fd指向的文件中,读取count个字节到buf中; 如果read成功,则返回读到的字节数,如果已达到结尾,则返回0,出错,返回-1 函数原型:

 

//头文件
#include <unistd.h>
//原型
ssize_t read(int fd, void *buf, size_t count);

参数说明:
 

fd:文件描述符
buf:保存读入信息的缓存
count:要读取的字节数

答案:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define rwmode 0
int main()
{
    int fd;
    char buffer[1024];
    int n;
    fd = open("/data/workspace/myshixun/case1/testFIle", rwmode);
    if (fd < 0)
    {
        printf("Open file error!\n");
        exit(1);
    }
    else
        printf("open testFIle ok!\n");
    //请使用read函数将其读入buffer中
    n = read(fd,buffer,1023);
    buffer[n] = '\0';
    printf("%s\n", buffer);
    close(fd);
    return 0;
}

第1关:写文件系统函数

任务描述

本关任务:使用write()函数编写一个写文件C程序。

相关知识

为了完成本关任务,你需要掌握: 1.如何使用C语言读取文件; 2.如何使用C语言写入文件。

write函数

write系统调用将buf所指向的缓冲区的count个字节内容写入fd指向的文件; 如果write成功,则返回写入的字节数,出错返回-1 函数原型:

//头文件
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

参数说明:

fd:要写入的文件
buf:要写入的信息所在的缓存
count:要写入的字节数

答案:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define resource_mode 0
#define destination_mode 0774
#define FILESIZE 1024

int main(int argc, char *argv[])
{
    int resource_fd, destination_fd;
    char buffer[FILESIZE], *p;
    int readbytes, writebytes;
    if (argc != 3)
    {
        printf("Usage:copy from resource file to destination file\n %s src_file dest_file\n", argv[0]);
        exit(0);
    }
    if ((resource_fd = open(argv[1], resource_mode)) == -1)
    {
        perror("Can't open source file");
        exit(0);
    }
    if ((destination_fd = creat(argv[2], destination_mode)) == -1)
    {
        perror("Can't create destination file");
        exit(0);
    }
    // 请使用read函数读取前1024字节的内容读到缓冲区buffer中
    while ((readbytes = read(resource_fd,buffer,FILESIZE))>0)
    {
        p = buffer;
        if ((readbytes == -1) && (errno != EINTR))
            break;
        else if (readbytes > 0)
        {
            // 请使用write函数读取到的前1024字节的内容写到目的文件中
            while ((writebytes = write(destination_fd,p,readbytes))>0)
            {
                if ((writebytes == -1) && (errno != EINTR))
                    break;
                else if (writebytes == readbytes)
                    break;
                else if (writebytes > 0)
                {
                    p += writebytes;
                    readbytes -= writebytes;
                }
            }
            if (writebytes == -1)
                break;
        }
    }
    close(resource_fd);
    close(destination_fd);
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

星与星熙.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值