c - C 中的分割文件错误在 readfile 中获取缓冲区

标签 c file file-io split

我在Ubuntu中用C编写了一个分割文件的程序。在读取文件中获取缓冲区时出现错误。

这是我的代码:

    int split(char *filename, unsigned long part) {
        FILE *fp;
        char *buffer;
        size_t result; // bytes read
        off_t fileSize;
    
        fp = fopen(filename, "rb");
        if (fp == NULL) {
            fprintf(stderr, "Cannot Open %s", filename);
            exit(2);
        }
    // Get Size
        fileSize = get_file_size(filename);
    // Buffer
        buffer = (char*) malloc(sizeof(char) * (fileSize + 1));
        if (buffer == NULL) {
            fputs("Memory error", stderr);
            fclose(fp);
            return 1;
        }
    // Copy file into buffer
    //char buffers[11];
        result = fread(buffer, 1, fileSize, fp);
        buffer[fileSize] = '\0';
    
        if (result != fileSize) {
            fputs("Reading error", stderr);
            return 1;
        }
    
    // Split file
        off_t partSize = fileSize / part;
    // Last Part
        off_t lastPartSize = fileSize - partSize * part;
        unsigned long i;
        unsigned long j;
        // create part 1 to n-1
        for (j = 0; j < part; j++) {
            char partName[255];
            char *content;
            char partNumber[3];
            // Content of file part
    //      for (i = j; i < partSize * (j + 1); i++) {
    //
    //      }
            content = (char*) malloc(sizeof(char) * partSize);
            content = copychar(buffer, j + i, partSize + i);
            i += partSize;
            //copy name
            strcpy(partName, filename);
            // part Number
            sprintf(partNumber, "%d", j);
            // file name with .part1 2 3 4 ....
            strcat(partName, ".part");
            strcat(partName, partNumber);
            // Write to file
            writeFile(partName, content);
            free(content);
        }
    // last part
    char *content;
    content = (char*) malloc(sizeof(char) * (fileSize - partSize * (part - 1)));
    content = copychar(buffer, (part - 1) * partSize + 1, fileSize);
    char lastPartNumber[3];
    char lastPartName[255];
    sprintf(lastPartNumber, "%d", part);
    strcpy(lastPartName, filename);
    strcat(lastPartName, ".part");
    strcat(lastPartName, lastPartNumber);
    writeFile(lastPartName, content);
    free(content);

    free(buffer);
    fclose(fp);
    return 0;    
}

这是函数 copychar 从开始到结束的内容:

char *copychar(char* buffer, unsigned long start, unsigned long end) {
    if (start >= end)
        return NULL;
    char *result;
    result = (char*) malloc(sizeof(char) * (end - start) + 1);
    unsigned long i;
    for (i = start; i <= end; i++)
        result[i] = buffer[i];
    result[end] = '\0';
    return result;
}

这是获取文件大小的函数:

off_t get_file_size(char *filename) {
    struct stat st;
    if (stat(filename, &st) == 0)
        return st.st_size;
    fprintf(stderr, "Cannot determine size of %s: %s\n", filename);
    return -1;
}

这是写入文件的函数:

    int writeFile(char* filename, char*buffer) {
    if (buffer == NULL || filename == NULL)
        return 1;
    FILE *file;
    file = fopen(filename, "wb");
    fwrite(buffer, sizeof(char), sizeof(buffer) + 1, file);
    fclose(file);
    return 0;
}

当我测试时,我使用了 29MB 的测试文件,然后它被转储了。我调试并返回 fileSize true,但是当从文件中读取缓冲区中的文件时,它仅返回 135 个字符,而当使用 copychar 时,它会出错。

    Breakpoint 1, 0x0000000000400a0b in copychar (buffer=0x7ffff5e3a010 "!<arch>\ndebian-binary   1342169369  0     0     100644  4         `\n2.0\ncontrol.tar.gz  1342169369  0     0     100644  4557      `\n\037\213\b", start=4154703576, end=4164450461) at final.c:43

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a0b in copychar (buffer=0x7ffff5e3a010 "!<arch>\ndebian-binary   1342169369  0     0     100644  4         `\n2.0\ncontrol.tar.gz  1342169369  0     0     100644  4557      `\n\037\213\b", start=4154703576, end=4164450461) at final.c:43

程序因信号 SIGSEGV(段错误)而终止。 该程序不再存在。

如何将缓冲区分成几部分,以便在分割时写入部分?

最佳答案

您可能已经注意到,将文件复制到一大块中是非常不切实际的。而且不需要。

在最简单的级别上,您可以逐字节复制文件,如下所示

while( ( ch = fgetc(source) ) != EOF ) {
   fputc(ch, target);
}

这会起作用,但会很慢。最好分块复制,如下所示:

 unsigned char buf[4096];
 size_t size;
 while( (size = fread(buf, 1, sizeof(buf), fpRead) ) > 0) {
     fwrite(buf, 1, size, fpWrite);
 }

请注意,生成的代码要简单得多,并且不包含动态内存分配。

当然,您仍然需要添加拆分逻辑,但这可以通过跟踪写入的字节数并在实际写入之前打开一个新的写入文件来完成。

编辑:如何处理多部分方面 - 示意性地讲,您仍然需要对某些特殊情况进行额外检查,当然还需要测试不同系统调用的结果

 unsigned char buf[4096];
 size_t size;
 size_t partsize = 100000; // asssuming you want to write 100k parts.
 size_t stilltobewritten = partsize; // bytes remaining to be written in current part
 size_t chunksize = sizeof(buf); // first time around we read full buffersize
 while( (size = fread(buf, 1, chunksize, fpRead) ) > 0) {
     fwrite(buf, 1, size, fpWrite);
     stilltobewritten -= size; // subtract bytes written from saldo
     if (stilltobewritten == 0) {
         // part is complete, close this part and open next
         fclose(fpWrite);
         fpWrite = fopen(nextpart,"wb");
         // and reinit variables
         stilltobewritten = partsize;
         chunksize = sizeof(buf);
     } else {
         // prep next round on present file - just the special case of the last block
         // to handle
         chunksize = (stilltobewritten > sizeof(buf)) ? sizeof(buf) : stilltobewritten;
     }
 }

和编辑2:文件部分名称也可以变得更简单:

 sprintf(partName, "%s.part%d",file, j);

关于c - C 中的分割文件错误在 readfile 中获取缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11702072/

相关文章:

c++ - 无法在 Linux 上使用 C++ 保存文本文件

在 URL 文件扩展名中使用 CAPS 时,Php 文件在 Palapa 网络服务器(Lighttpd 服务器)中公开

Perl 在 Windows 中使用外来字符

java - Android - 让用户浏览内部存储文件

c - 在 C 中用 fread 和 fwrite 提问

C free() 例程和递增的数组指针

c - 在 C 中打印字符串

java - 创建一个长度由用户输入 : C vs C99 vs Java 决定的数组

Android-保存文件(输入和输出流)抛出异常(打开失败 : EINVAL (Invalid argument)) in Pre-lollipop devices

c++ - 混合 C 和 C++ 代码时, main() 是否需要在 C++ 部分?