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.