Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将内容从一个文件放入另一个文件不起作用_C_File - Fatal编程技术网

C 将内容从一个文件放入另一个文件不起作用

C 将内容从一个文件放入另一个文件不起作用,c,file,C,File,我正在写一个程序,它接收一个文件内容并将其反转,我想将反转后的输出放入output.txt文件中,但没有任何内容被复制到output.txt中 内容正常,但未复制到另一个文件。(我创建了一个“output.txt”文件,希望将原始文本复制到该文件中。以下是我的代码: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]){ FILE *fp, *fp2; char

我正在写一个程序,它接收一个文件内容并将其反转,我想将反转后的输出放入output.txt文件中,但没有任何内容被复制到output.txt中 内容正常,但未复制到另一个文件。(我创建了一个“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;
}
#包括
#包括
int main(int argc,char*argv[]){
文件*fp,*fp2;
char ch;
INTA;
字符c;
int-s;
int i,pos;
int-choice=0;
fp=fopen(argv[1],“r”);
如果(fp==NULL)
{
printf(“文件不存在\n”);
}
fp2=fopen(argv[2],“w”);
如果(fp2==NULL){
printf(“ERRORRR”);
出口(1);
}
fseek(fp,0,SEEK_END);
pos=ftell(fp);
i=0;
while(i
我认为您必须以“写入模式”打开第二个文件

正如@Sami Kuhmonen所说的,
fputc(fp,fp2);
行有问题,因为
fputc
包含两个参数

fputc(int character, File *stream);
试试这个:

while (c){
     c=getc(fp1);
     fputc(c,fp2);
}

这对你来说是不是很奇怪:
fputc(fp,fp2)
?我对c相当陌生,尤其是在处理文件时,但我确实得到了一个编译器警告@SamiKuhmonenFirst..你不能在一个没有以二进制模式打开的文件中这样寻找(
rb
)。必须打开输出以进行写入,
w
。A需要从编译器获得的所有输出。好的,我将其更改为写入模式,但它仍然无法工作。您是否看到程序中存在任何其他缺陷?我找到了它!我最终执行了fputc(ch,fp2);参数顺序错误。
while (c){
     c=getc(fp1);
     fputc(c,fp2);
}