Ich versuche ein Programm zu erstellen, das eine Reihe von Dateien nimmt und sie in eine andere kopiert.Eine Liste von Dateien in eine andere zusammenführen
zum Beispiel
./foobar arch1.txt arch2.txt arch3.txt
muss mit dem Inhalt von arch1.txt arch2.txt erstellen arch3.txt, archN.txt.
Dies ist mein Code:
#include <stdio.h>
#include <stdlib.h>
void usage (char *argv[], int code)
{
printf("usage: %s [<file> <out_file>] \n", argv[0]);
exit(code);
}
void copyFile (FILE *ifp, FILE *ofp)
{
int c;
while ((c = fgetc(ifp)) != EOF)
fputc(c, ofp);
}
int main(int argc, char *argv[])
{
system ("clear");
FILE *fp, *fp2;
if (argc < 3)
usage(argv, EXIT_FAILURE);
else
if ((fp2 = fopen(argv[argc-1], "w")) == NULL) {
printf("Can't open file to write: %s\n", *argv);
exit(EXIT_FAILURE);
}
while(--argc > 0)
printf("%d",argc);
if ((fp = fopen(*++argv, "r")) == NULL) {
printf("Can't open file: %s\n", *argv);
exit(EXIT_FAILURE);
}
else {
copyFile(fp, fp2);
fclose(fp);
fclose(fp2);
}
return 0;
}
Mein ouput:
Datei kann nicht geöffnet schreiben: ./foobar
Siehe http://stackoverflow.com/a/3898070/5781248 –