无法反转转储输入文件

发布于 2024-11-02 07:41:26 字数 1252 浏览 0 评论 0原文

我想反转输入文件的内容并显示反转的内容,但我没有得到它;我想我犯了一个逻辑错误。

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>

int main() {
    char* c = malloc(10);
    char* c1 = malloc(10);
    char ch, arg1[100], arg2[100];
    int i, count = 0;
    FILE *fp, *fq;
    printf("Name of the file:");
    scanf("%s", arg1);
    fp = fopen(arg1, "w+");
    if (!fp) {
        perror("Failed to open file");
        return errno;
    }

    printf("\t\t\t%s\n\n", arg1);
    printf("\t\tInput the text into the file\n");
    printf("\t\tPress Ctrl+d to the stop\n");
    while ((*c=getchar()) != EOF) {
        fwrite(c, 1, sizeof(c), fp);
        count++;
    }

    printf("\n\n");
    fclose(fp);
    fp = fopen(arg1, "w+");
    printf("Name of the output file:");
    scanf("%s", arg2);
    printf("Reversing the contents of the file.......\n");
    fq = fopen(arg2, "w+");
    printf("\t\t%s\n\n", arg2);
    for (i = 1; i <= count; i++) {
        fseek(fp, -(i + 1), SEEK_END)
        fwrite(c1, 1, sizeof(c1), fq);
    }
    printf("Done....Opening the file\n");
    rewind(fq);
    for (i = 0; i <= count; i++) {
        ch = getc(fp);
        putc(ch, stdout);
    }
    fclose(fp);
    fclose(fq);
    return 0;
}

I want to reverse the contents of the input file and display the reversed contents, but I am not getting it; i think I have made a logic error.

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>

int main() {
    char* c = malloc(10);
    char* c1 = malloc(10);
    char ch, arg1[100], arg2[100];
    int i, count = 0;
    FILE *fp, *fq;
    printf("Name of the file:");
    scanf("%s", arg1);
    fp = fopen(arg1, "w+");
    if (!fp) {
        perror("Failed to open file");
        return errno;
    }

    printf("\t\t\t%s\n\n", arg1);
    printf("\t\tInput the text into the file\n");
    printf("\t\tPress Ctrl+d to the stop\n");
    while ((*c=getchar()) != EOF) {
        fwrite(c, 1, sizeof(c), fp);
        count++;
    }

    printf("\n\n");
    fclose(fp);
    fp = fopen(arg1, "w+");
    printf("Name of the output file:");
    scanf("%s", arg2);
    printf("Reversing the contents of the file.......\n");
    fq = fopen(arg2, "w+");
    printf("\t\t%s\n\n", arg2);
    for (i = 1; i <= count; i++) {
        fseek(fp, -(i + 1), SEEK_END)
        fwrite(c1, 1, sizeof(c1), fq);
    }
    printf("Done....Opening the file\n");
    rewind(fq);
    for (i = 0; i <= count; i++) {
        ch = getc(fp);
        putc(ch, stdout);
    }
    fclose(fp);
    fclose(fq);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2024-11-09 07:41:26

这是一个示例程序,它将文件加载到内存中,然后将内存的内容向后打印到标准输出。

#include <stdio.h>
#include <stdlib.h>

/* get the size of the file. No error checking here! */
long get_filesize(FILE *fp)
{
    long fsize;

    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);
    rewind(fp);

    return fsize;
}

int main(int argc, char *argv[])
{
    if(argv[1] == NULL) return EXIT_FAILURE;

    FILE *input;
    unsigned char *data;
    long filesize;
    int i;

    /* open target file */
    input = fopen(argv[1], "rb");
    if(input == NULL) exit(EXIT_FAILURE);

    /* retrieve size of the file */
    filesize = get_filesize(input); 
    if(filesize < 1) exit(EXIT_FAILURE);

    /* allocate space for the file */
    data = malloc(filesize * sizeof(unsigned char));
    if(data == NULL) exit(EXIT_FAILURE);

    /* read the file into buffer and close the file handle */
    fread(data, filesize, sizeof(unsigned char), input);
    fclose(input);

    /* print the file content from end to beginning */
    for(i = --filesize; i >= 0; --i)
        putchar(data[i]);

    /* free the data buffer memory */
    free(data);

    return EXIT_SUCCESS;
}

输入文本:

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen book.

输出文本:

.koob nemiceps epyt a ekam ot ti delbmarcs dna epyt fo yellag a koot retnirp
nwonknu na nehw ,s0051 eht ecnis reve txet ymmud dradnats s'yrtsudni eht neeb
sah muspI meroL .yrtsudni gnittesepyt dna gnitnirp eht fo txet ymmud ylpmis si
muspI meroL

Here is an example program which loads the file into memory and then prints the content of the memory backwards to stdout.

#include <stdio.h>
#include <stdlib.h>

/* get the size of the file. No error checking here! */
long get_filesize(FILE *fp)
{
    long fsize;

    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);
    rewind(fp);

    return fsize;
}

int main(int argc, char *argv[])
{
    if(argv[1] == NULL) return EXIT_FAILURE;

    FILE *input;
    unsigned char *data;
    long filesize;
    int i;

    /* open target file */
    input = fopen(argv[1], "rb");
    if(input == NULL) exit(EXIT_FAILURE);

    /* retrieve size of the file */
    filesize = get_filesize(input); 
    if(filesize < 1) exit(EXIT_FAILURE);

    /* allocate space for the file */
    data = malloc(filesize * sizeof(unsigned char));
    if(data == NULL) exit(EXIT_FAILURE);

    /* read the file into buffer and close the file handle */
    fread(data, filesize, sizeof(unsigned char), input);
    fclose(input);

    /* print the file content from end to beginning */
    for(i = --filesize; i >= 0; --i)
        putchar(data[i]);

    /* free the data buffer memory */
    free(data);

    return EXIT_SUCCESS;
}

Input text:

Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type
specimen book.

Output text:

.koob nemiceps epyt a ekam ot ti delbmarcs dna epyt fo yellag a koot retnirp
nwonknu na nehw ,s0051 eht ecnis reve txet ymmud dradnats s'yrtsudni eht neeb
sah muspI meroL .yrtsudni gnittesepyt dna gnitnirp eht fo txet ymmud ylpmis si
muspI meroL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文