【问题标题】:failing to dump the input file reversed未能转储输入文件反转
【发布时间】:2011-04-19 16:07:53
【问题描述】:

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

#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;
}

【问题讨论】:

  • 你得到什么输出?你试图做什么来解决这个问题?你怎么知道它不起作用?
  • 如果文件的内容很小,您可能希望将所有内容加载到内存中,然后将其反转。 fseek(...)rewind() 在我看来效率极低。
  • 我已经在我的 linux 上编译了程序,我无法得到反转内容的输出
  • 我同意你的观点,但我这样做只是为了解决问题

标签: c c89


【解决方案1】:

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

#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

【讨论】:

  • @Athabaska,您的程序访问分配给data 的缓冲区之外的内存——您需要一个- 1 在某处。在malloc 调用中也没有理由乘以sizeof(unsigned char) - 它总是会是1
  • 已经修复。这个stackoverflow系统很讨厌。当我修复错误时,总是有人同时出现。但没有冒犯。很高兴有人发现了这些讨厌的 C 错误。至于sizeof(unsigned char),通常我添加它只是为了便于携带。我认为这是一个很好的做法。
  • @Athabaska - 没有汗水,只是想帮忙!
  • 我们如何使用 data[i] 因为 data 是指向单个 char 的指针,但编译器不显示错误
  • 我厌倦了每个人都在抱怨这一点。我个人也像许多其他程序员一样添加了for clarity。在 stackoverflow 上有关于这个主题的无休止的激烈争论。
猜你喜欢
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2018-08-15
相关资源
最近更新 更多