-5

i have a code that prints the content of a txt file reversely as a string.İ store the every characters of my string in the txt file by using malloc() function.İt works properly but it prints the content of the with a preceding '\0' What is the reason?

void veriyitersyazdir(FILE *file)
{
    char metin;          // that gets the every single char of my string
    int boyutsayac=0;    // that stores my string's size
    char *metinarr=NULL; // that is my malloc() array pointer
    int dongusayac;      // that is a counter variable that is used to print my string reversely.

    system("cls");

    file = fopen("C:\\metin.txt", "r");

    do {
        metin = fgetc(file);
        if (metin==EOF) {
            break;
        }

        boyutsayac++;
    } while (1);

    metinarr = (char *) malloc(sizeof(char)*boyutsayac);
    fseek( file, 0, SEEK_SET);
    for (dongusayac = 0; dongusayac < boyutsayac; dongusayac++) {
        metin = fgetc(file);
        metinarr[dongusayac]= metin;
    }

    fseek( file, 0 , SEEK_SET);
    for (; dongusayac >=0; dongusayac--) {
        printf("%c", metinarr[dongusayac]);
    }

    fclose(file);
}

content of the txt file:Mustafa Mutlu

the output of the codes: UltuM afatsuM

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
  • 1
    `for (; dongusayac >=0; dongusayac--)` : `dongusayac` begins with one greater value. – BLUEPIXY May 03 '16 at 06:01
  • 1
    `metin = fgetc(file);` where `metin` is a **char** is utterly wrong. See http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc/35356684#35356684. `fgetc` returns an **int**, you should honour that contract, because a **char** possibly cannot represent EOF (on systems where char is unsigned). It will still be wrong on systems with signed char too, just a bit differently. – Ilja Everilä May 03 '16 at 06:11
  • This would be why you should always write source code in English. Because you might need help with it eventually. – Lundin May 03 '16 at 06:43

1 Answers1

1

Your main problem, as noted by bluepixy, comes from the for loop that you use to read which increments dongusayac one more than the number of characters read.

That is always the case in a for loop as it increases the variable and then checks if it still fits the condition. So the last increase never fits the condition.

Here is a cleaner way to do what you're doing:

void reverse() {
    FILE *file;
    size_t size;
    int i;

    if (!(file = fopen("c:\\metin.txt", "r"))) {
            printf("error opening file\n");
            exit(1);
    }

    fseek(file, 0L, SEEK_END);
    size = ftell(file);

    char *content = malloc(size);           // allocate memory for the full text file
    fseek(file, 0, SEEK_SET);               // rewind the file cursor
    fread(content, 1, size, file);

    for (i = size - 1; i >= 0; i --) {      // output contents in reverse order
            printf("%c", content[i]);
    }

    fclose(file);
}
Julie Pelletier
  • 1,740
  • 1
  • 10
  • 18