简体   繁体   English

将两个文件合并为C中的第三个文件(fread和fwrite)

[英]Merging two files into a third in C (fread and fwrite)

I'm trying to make a function that takes a series of files and writes the contents of each one into a new one. 我正在尝试制作一个函数,该函数需要一系列文件,并将每个文件的内容写入一个新文件。

#include <stdio.h>
#include <stdlib.h>

    void file_copy(FILE *f1, FILE *f2) {
    char buffer[BUFFER_SIZE];
    size_t sz;
    sz = fread(buffer, sizeof(buffer),1,f1);
    while (sz == 1) {
        fwrite(buffer, sz,1,f2);
        sz = fread(buffer, sizeof(buffer),1,f1);
    }
}

int main(int argc, char const *argv[])
{
    FILE *f_in, *f_out;
    int i;
    if (argc < 3)
    {
        puts("Not enough arguments.");
        exit(1);
    }

    if ((f_out = fopen(argv[argc-1], "w")) == NULL)
    {
        printf("Can't open %s for writing.\n", argv[argc-1]);
        exit(1);
    }

    for (i = 0; i < argc-1; ++i)
    {
        if ((f_in = fopen(*++argv, "r")) == NULL)
        {
            printf("Can't open %s for reading\n", *argv);
            exit(1);
        }
        else
            file_copy(f_in, f_out);
        close(f_in);
        close(f_out);
    }


    return 0;
}

and I dont' get any output in my output file. 而且我的输出文件中没有任何输出。 It seems to be closing them just right. 似乎正好将它们关闭。

There are Several fails: 有几个失败:

  1. buffer must be allocated somehow 必须以某种方式分配缓冲区
  2. write have to write number of bytes read, not full buffer write必须写入读取的字节数,而不是完整的缓冲区

So it should be like this: 所以应该是这样的:

#define BUF_SIZE 1024

void file_copy(FILE *f1, FILE *f2) {
    char buffer[BUF_SIZE];

    while (!feof(f1)) {
        size_t r = fread(buffer, 1, sizeof(buffer), f1);
        fwrite(buffer, 1, r, f2);
    }
}

Did you plan to save the text from the list of files in the last file? 您是否打算将文件列表中的文本保存到最后一个文件中? It isn't really clear. 目前还不清楚。 There are multiple problems with the code aside from unreadable syntax. 除了不可读的语法外,代码还有很多问题。 You should be using fclose instead of close . 您应该使用fclose而不是close And reading a number of one byte elements instead of one element of a giant array. 并读取多个一个字节的元素,而不是一个巨型数组的一个元素。

Here is the fixed code: 这是固定代码:

#include <stdio.h>
#include <stdlib.h>

void file_copy(FILE *f1, FILE *f2) {
    char buffer[1024];
    ssize_t sz;

    while (!feof(f1)) {
        sz = fread(buffer, 1, sizeof(buffer), f1);

        fwrite(buffer, 1, sz, f2);
    }
}

int main(int argc, char const *argv[])
{
    FILE *f_in, *f_out;
    int i;
    if (argc < 3)
    {
        puts("Not enough arguments.");
        exit(1);
    }

    if ((f_out = fopen(argv[argc-1], "w")) == NULL)
    {
        printf("Can't open %s for writing.\n", argv[argc-1]);
        exit(1);
    }

    for (i = 1; i < argc-1; i++)
    {
        if ((f_in = fopen(argv[i], "r")) == NULL)
        {
            printf("Can't open %s for reading\n", *argv);
            exit(1);
        }
        else
        {
            file_copy(f_in, f_out);
            fclose(f_in);
        }
    }
    fclose(f_out);

    return 0;
}

You should definitely know how much to write, because you end up writing 1024 bytes no matter what, as is now. 您绝对应该知道要写多少,因为无论现在如何,最终无论如何写1024字节。

Depending on what file type you are dealing with, this is an option for txt-files at least: 根据要处理的文件类型,这至少是txt文件的一种选择:

void file_copy(FILE *f1, FILE *f2) {

    fseek(f1, 0, SEEK_END);
    int buf_size = ftell(f1);
    char buf[buf_size];

    //Reset file position
    fseek(f1, 0, SEEK_SET);
    sz = fread(buf, sizeof(buf), 1, f1);
    fwrite(buf, sz, 1, f2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM