将文件列表合并到另一个文件列表中
Merging a list of files into another
我正在尝试制作一个程序,它可以获取一系列文件并将它们复制到另一个文件中。
例如
./foobar arch1.txt arch2.txt arch3.txt
必须使用 arch1.txt arch2.txt、archN.txt.
的内容创建 arch3.txt
这是我的代码:
#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;
}
我的输出:
Can't open file to write: ./foobar
那是因为您在 fopen() 中使用 *argv 作为文件名参数。应该是 argv[argc - 1].
*argv
不是第一个参数,而是可执行文件的路径。
在直接使用之前将 argv 自增一次:
argv++ ;
if ((fp2 = fopen(argv[argc-1], "w")) == NULL) {
或者更好地使用数组索引并从 argv[1]
.
开始
我修好了。它不是很漂亮,但现在可以用了。
#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[])
{
FILE *f_read, *f_write;
int i;
if (argc < 3)
usage(argv, EXIT_FAILURE);
if ((f_write = fopen(argv[argc-1], "w")) == NULL) {
printf("Can't write in: %s\n", argv[argc-1]);
exit(EXIT_FAILURE);
}
for (i = 1; i < argc-1; ++i)
{
if ((f_read = fopen(argv[i], "r")) == NULL) {
printf("Can't open file: %s\n", argv[i]);
exit(EXIT_FAILURE);
}
else
copyFile(f_read, f_write);
}
fclose(f_read);
fclose(f_write);
return 0;
}
我正在尝试制作一个程序,它可以获取一系列文件并将它们复制到另一个文件中。
例如
./foobar arch1.txt arch2.txt arch3.txt
必须使用 arch1.txt arch2.txt、archN.txt.
的内容创建 arch3.txt这是我的代码:
#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;
}
我的输出:
Can't open file to write: ./foobar
那是因为您在 fopen() 中使用 *argv 作为文件名参数。应该是 argv[argc - 1].
*argv
不是第一个参数,而是可执行文件的路径。
在直接使用之前将 argv 自增一次:
argv++ ;
if ((fp2 = fopen(argv[argc-1], "w")) == NULL) {
或者更好地使用数组索引并从 argv[1]
.
我修好了。它不是很漂亮,但现在可以用了。
#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[])
{
FILE *f_read, *f_write;
int i;
if (argc < 3)
usage(argv, EXIT_FAILURE);
if ((f_write = fopen(argv[argc-1], "w")) == NULL) {
printf("Can't write in: %s\n", argv[argc-1]);
exit(EXIT_FAILURE);
}
for (i = 1; i < argc-1; ++i)
{
if ((f_read = fopen(argv[i], "r")) == NULL) {
printf("Can't open file: %s\n", argv[i]);
exit(EXIT_FAILURE);
}
else
copyFile(f_read, f_write);
}
fclose(f_read);
fclose(f_write);
return 0;
}