【问题标题】:How to read a specified number of bytes from a specified offset from a file [closed]如何从文件的指定偏移量读取指定数量的字节[关闭]
【发布时间】:2020-05-04 21:22:40
【问题描述】:

我想从文件的指定偏移值中读取指定数量的字节。我将偏移量、字节数、文件名作为参数传递给文件。下面的代码不起作用。有人可以帮我吗?谢谢。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<error.h>
#include<errno.h>
#include<fcntl.h>
#define buf_size 512

int main(int argc, char *argv[])
{
    int bytes;
    int offset;
    int fd;
    int i = 0;
    int length = 0;
    ssize_t read_bytes;
    char *file;
    char buf[buf_size];

    if (argc != 4)
        error(1, 0, "Too many or less than the number of arguments");
    file = argv[1];
    offset = atoi(argv[2]);
    bytes = atoi(argv[3]);
    fd = open(file, O_RDONLY);
    if (fd == -1)
        error(1, errno, "Error while opening the file\n");
    while (1) {
        read_bytes = read(fd, buf, bytes);
        if (read_bytes == -1)
            error(1, errno, "Error while reading the file\n");
        length += read_bytes;
        printf("The length is : %d\n", length);
        if (length >= offset) {
            for (i = length ; i < bytes; i++)
                putchar(buf[i]);
            break;
        }
    }
    if (close(fd) == -1)
        error(1, 0, "Error while closing the file\n");

}

【问题讨论】:

  • 查找lseek
  • 另外,想想如果offset不是bytes的倍数,或者bytes大于buf_size,你的程序会做什么。
  • 问题描述“下面的代码不起作用”不是我们可以帮助您的。你的输入是什么?你的代码的输出是什么?你期望从中得到什么输出?请阅读How to Ask
  • @Tsyvarev 好的,当然。抱歉这个错误。不会再重蹈覆辙。谢谢。

标签: c linux unix operating-system


【解决方案1】:

由于提问者指出的限制,我决定将一些 POSIX 函数替换为 C 标准库中的函数。这是相当便携的。

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#define buf_size 512

int main(int argc, char *argv[])
{
    int bytes;
    int offset;
    FILE *fd;
    int i = 0;
    ssize_t read_bytes;
    char *file;
    char buf[buf_size];

    if (argc != 4)
        printf("Too many or less than the number of arguments\n");
    file = argv[1];
    offset = atoi(argv[2]);
    bytes = atoi(argv[3]);
    fd = fopen(file, "r");
    if (fd == NULL)
        perror("ERROR");
    fseek(fd, offset, SEEK_SET);
    read_bytes = fread(buf, 1, bytes, fd);
    if (read_bytes == 0) {
        if (feof(fd))
            printf("ERROR: End of file.\n");
        else if (ferror(fd))
            printf("ERROR: Unable to read the file.\n");
        return 1;
    }
    for (i = 0; i < bytes; i++)
        putchar(buf[i]);
    if (fclose(fd) == EOF)
        perror("ERROR:");
    return 0;

}

【讨论】:

  • 谢谢,但我有一个不使用 lseek 系统调用的限制。谢谢。
  • 好吧,改用 read() 本身,我已经编辑了我的答案。
  • 谢谢,但它不起作用。
  • 什么操作系统?这是我在 Ubuntu 上写的。
  • Debian 9 在 VirtualBox 中使用它。 @盯着
【解决方案2】:

我只对上述问题使用了 open、close、read、write 系统调用。我认为它可以处理部分读取、偏移 > buf_size 和 bytes > buf_size。谢谢。

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<error.h>
#include<errno.h>
#include<fcntl.h>
#define buf_size 5

int main(int argc, char *argv[])
{   
    int bytes;
    int offset;
    int fd; 
    char *file;
    char buf[buf_size];
    int rlen = 0;
    int len = 0;
    int i = 0;

    if (argc != 4)
        error(1, 0, "Too many or less than the number of arguments");
    file = argv[1];
    offset = atoi(argv[2]);
    bytes = atoi(argv[3]);
    fd = open(file, O_RDONLY);
    printf("The values of the file descriptor is : %d\n", fd);
    if (fd == -1)
        error(1, errno, "Error while opening the file\n");

    while (1) {
        rlen = read(fd, buf, offset);
        if (rlen == -1)
            error(1, errno, "Error in reading the file\n");
        len = len + rlen;
        if(len == offset) {
            len = 0;
            while (1) {
                rlen = read(fd, buf, bytes);
                if (rlen == -1)
                    error(1, errno, "Error in reading the file\n");
                if (rlen == 0)
                    return 0;
                len = len + rlen;
                for (i = 0; i < rlen; i++) 
                    putchar(buf[i]);
                if (len == bytes) {
                    printf("\nSuccess\n");
                    return 0;
                }
                bytes = bytes - rlen;
            }
        }
    }
}

【讨论】:

  • @Nate Eldredge 能否请您查看一下。非常感谢。
  • @strance 感谢您的帮助。你介意检查一下吗?
猜你喜欢
  • 2012-11-30
  • 2023-01-27
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多