3つのコマンドライン引数を取るプログラムを作成しようとしています。1.既存のファイル名2.新しいfile、3.各行から新しいファイルにコピーする文字数。ここで1行のファイルから特定の行に特定の文字数をコピーする方法
は、私がこれまで持っているものです:事前に
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]から開始しますか? –