ファイルの内容を取り込み、それを元に戻すプログラムを書いて、output.txtファイルに逆の出力を入れたいが、何もoutput.txtにコピーされない。 コンテンツは正常に逆転していますが、もう一方のファイルにはコピーされません。 (私はorignialテキストはにコピーしたい「output.txtと」ファイルを作成しているここに私のコードです:。あるファイルの内容を別のファイルに入れようとすると、
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
FILE *fp, *fp2;
char ch;
int a;
char c;
int s;
int i, pos;
int choice = 0;
fp = fopen(argv[1],"r");
if (fp == NULL)
{
printf("file doesnt exist \n");
}
fp2 = fopen(argv[2], "w");
if(fp2 == NULL) {
printf("ERRORRR");
exit(1);
}
fseek(fp,0,SEEK_END);
pos=ftell(fp);
i = 0;
while (i < pos){
i++;
fseek(fp,-i,SEEK_END);
ch = fgetc(fp);
printf("%c", ch);
fputc(fp, fp2);
}
/*
do {
a = fgetc(fp);
fputc(a,fp2);
}
while (a != EOF);
*/
return 0;
}
これは奇妙に見えますか?fputc(fp、fp2) '? –
私はかなり新しくcでファイルに取り組んでいますが、コンパイラ警告@SamKuhmonenを取得します – Alopax
まずは、 ['fgetc'は** int' **]を返します(https://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc)。 **バイナリモード**(** 'rb' **)で開かれていないファイルでは、このように求めることはできません。出力は書き込みのために開かなければなりません、 'w'。 –