2

我需要使用递归在终端中向后写出 .txt 文件,但似乎我被卡住了。到目前为止,这是我的代码,但它创建了一个无限循环。此外,过程write()应该只有 1 个参数 - 指向文件的指针。

#include <stdio.h>

void write(FILE **f)
{
    char cur;

    fseek(*f,-1,SEEK_CUR);
    cur = fgetc(*f);

    printf("%c",cur);
    write(f);
}

int main(void)
{
    FILE *f;
    f = fopen("text.txt","r");
    fseek(f, 0, SEEK_END);
    write(&f);
    
    fclose(f);
    return 0;
}

我想使用fseekftell函数,它们似乎是最好的方法。我的预期输出是: 文件 Hello world 输出 dlrow olleH

4

2 回答 2

0

这个任务实际上不是很适合递归但是..

编辑我实际上误解了你的问题,但它同样微不足道。

#include <stdio.h>
#include <string.h>

void writeRecursive(FILE *file)
{
    int ch;
    if(fread(&ch, 1, 1, file) == 1) 
    { 
        writeRecursive(file);
        fwrite(&ch, 1, 1, file);
    }
    else
    {
        rewind(file);
    }

}

int main(void)
{
    FILE *fi = fopen("a.txt", "w+");
    char c;

    fprintf(fi, "123456789");
    rewind(fi);

    writeRecursive(fi);
    rewind(fi);
    while(fread(&c, 1, 1, fi) == 1)
    {
        printf("%c", c);
    }
    printf("\n");
}

https://godbolt.org/z/edGfKa

或者

void writeRecursive(FILE *file)
{
    int ch;
    if((ch = fgetc(file)) != EOF) 
    { 
        writeRecursive(file);
        fputc(ch, file);
    }
    else
    {
        rewind(file);
    }

}

旧答案

void printRecursive(FILE *fi, FILE *fo)
{
    char ch;
    if(fread(&ch, 1, 1, fi) == 1) 
    { 
        printRecursive(fi,fo);
        fwrite(&ch, 1, 1, fo);
    }

}

int main(void)
{
    FILE *fi = stdin, *fo = stdout;

    printRecursive(fi, fo);
}

https://godbolt.org/z/YjfvcP

于 2021-01-02T13:58:25.030 回答
0

From what you have given, the way to do the problem recursively using fseek and ftell can be written as follows,

#include <stdio.h>

void write(FILE **f)
{
    char cur;
    cur = fgetc(*f);
    printf("%c", cur);
    if (ftell(*f) == 1) return;
    fseek(*f,-2,SEEK_CUR);
    write(f);
}

int main(void)
{
    FILE *f;
    f = fopen("text.txt","r");
    fseek(f, -1, SEEK_END);
    write(&f);
    
    fclose(f);
    return 0;
}

Considering the file is read in reverse, the stop condition requires a bit of thinking. The position of a pointer is returned by ftell and using this we can detect if the current position of the pointer is at the start of the file or not (as in if it is equal to 1).

There were tiny mistakes in your code - the recursion never stopped (as pointed out in the comments), make sure you always have a stop inside your recursive functions.

The order of your fseek needs changing so that you read the first character in properly.

And you need to use a -2 inside your fseek to move to the previous character. This is because of the fact that fgetc will advance the pointer by 1 and you will have to go back by 1 character to reach the character that was read just now and you then you want to move it backwards by one more, so that you get to the character before the one that was just read. Thanks to p-j-supports-women-in-poland for pointing this out.

Otherwise the above code will make it clear for you, I hope. if you still got doubts ping me in the comments. Happy to help :)

于 2021-01-02T14:21:13.010 回答