2011-04-19 76 views
0
#include<stdlib.h> 
#include<errno.h> 
#include<stdio.h> 
int main() 

{ 

    char *c=NULL,*c1,ch; 
    c=(char*)malloc(10*sizeof(char)); 
    c1=(char*)malloc(10*sizeof(char)); 
    char 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==NULL) 

    { 

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

我想扭轉輸入文件的內容,並希望顯示反轉的內容,但我沒有得到它;我認爲我犯了一個邏輯錯誤。沒有開始所需的輸出

+6

你得到什麼輸出?你有什麼試圖解決這個問題?你怎麼知道它不工作? – 2011-04-19 16:11:27

+0

如果文件內容很小,則可能需要將所有內容加載到內存中,然後在那裏將其反轉。那個'fseek(...)'和'rewind()'對我來說看起來效率非常低。 – abeln 2011-04-19 16:14:02

+0

我已經編譯了我的linux上的程序,我無法得到反轉內容的輸出 – 2011-04-19 16:14:50

回答

2

這是一個示例程序,它將文件加載到內存中,然後將內存的內容向後打印到標準輸出。

#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 
+0

@Athabaska,您的程序在分配的緩衝區外訪問內存對於'data' - 你需要一個' - 1'在那裏。在'malloc'調用中'sizeof(unsigned char)'也沒有任何理由 - 它總是會變成'1'。 – 2011-04-19 17:41:21

+0

已經修復。這個stackoverflow系統是討厭的。有人總是在我修正錯誤時同時發表評論。但沒有冒犯。很高興有人抓住這些令人討厭的C錯誤。至於'sizeof(unsigned char)',通常我只是爲了便攜性而添加它。我認爲這是一個很好的做法。 – 2011-04-19 17:46:33

+0

@Athabaska - 沒有汗水,只是想幫助! – 2011-04-19 17:48:55