写文件系统函数

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h> // Added for read() and write() functions

#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 the first 1024 bytes of content into the buffer using the read function
    while ((readbytes = read(resource_fd, buffer, FILESIZE)) > 0)
    {
        p = buffer;
        if (readbytes == -1 && errno != EINTR)
            break;
        else if (readbytes > 0)
        {
            // Write the read 1024 bytes of content to the destination file using the write function
            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;
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Zephyr支持多种文件系统,包括FAT和NAND Flash等。以下是一些常用的文件系统函数: 1. 打开文件: ```c int fs_open(struct fs_file_t *file, const char *path); ``` 这个函数用于打开文件,需要传入一个fs_file_t结构体指针和文件路径。如果成功打开文件,返回0,否则返回错误码。 2. 读取文件: ```c ssize_t fs_read(struct fs_file_t *file, void *buf, size_t nbytes); ``` 这个函数用于从文件中读取数据,需要传入一个fs_file_t结构体指针、缓冲区指针和要读取的字节数。如果成功读取数据,返回实际读取的字节数,否则返回错误码。 3. 文件: ```c ssize_t fs_write(struct fs_file_t *file, const void *buf, size_t nbytes); ``` 这个函数用于向文件入数据,需要传入一个fs_file_t结构体指针、数据指针和要入的字节数。如果成功入数据,返回实际入的字节数,否则返回错误码。 4. 关闭文件: ```c int fs_close(struct fs_file_t *file); ``` 这个函数用于关闭文件,需要传入一个fs_file_t结构体指针。如果成功关闭文件,返回0,否则返回错误码。 5. 删除文件: ```c int fs_unlink(const char *path); ``` 这个函数用于删除文件,需要传入文件路径。如果成功删除文件,返回0,否则返回错误码。 6. 创建目录: ```c int fs_mkdir(const char *path); ``` 这个函数用于创建目录,需要传入目录路径。如果成功创建目录,返回0,否则返回错误码。 7. 删除目录: ```c int fs_rmdir(const char *path); ``` 这个函数用于删除目录,需要传入目录路径。如果成功删除目录,返回0,否则返回错误码。 8. 获取文件信息: ```c int fs_stat(const char *path, struct fs_dirent *entry); ``` 这个函数用于获取文件信息,需要传入文件路径和一个fs_dirent结构体指针。如果成功获取文件信息,返回0,否则返回错误码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值