所以我想编写一个程序,需要3个命令行参数,1。现有文件的名称,2.新的名称文件,3.从每行复制到新文件的字符数。如何从每一行从一个文件复制字符的具体数目另一
这是我到目前为止有:提前
int main(int argc, char *argv[]) {
int size = atoi(argv[3]); // The number of characters to copy
char content[size];
char line[size];
FILE *f1 = fopen(argv[1], "r"); // Read from first file
FILE *f2 = fopen(argv[2], "w"); // Write to second file
if (f1 == NULL || f2 == NULL) {
printf("\nThere was an error reading the file.\n");
exit(1);
}
while (fgets(content, size, f1) != NULL) {
// This is what I had first:
fprintf(f2, "%s", content);
// And when that didn't work, I tried this:
strncpy(line, content, size);
fprintf(f2, "%s", line);
}
fclose(f1);
fclose(f2);
return 0;
}
谢谢!
什么是你的问题? – kaylum
请指出预期的输出示例。 – BLUEPIXY
不会argv从argv [0]开始而不是argv [1]? –