Search code examples
cfilefopeneoffclose

Program quits while changing characters?


I am trying to make a program that converts (Đ,Š,Č,Ć and Ž) Serbian(Latin) charecters to (D,S,C,C,Z) so my TV can recnognise them btw.
NO IT's NOT A ENCODING ERROR, YES MY TV IS RUNNING THE LATEST SOFTWARE.
So I decided to make this command program that converts characters.
The problem is the target file stays unchanged, and it quits after this message:

Počinje konvertovanje.

Why does this happen? The source:

#include <stdio.h>
#include <stdlib.h>
char c;
char filename[256];
int er;
int main()
{
    printf("\nUnesi podatak za prevoðenje:");
    scanf("%s",&filename);
    FILE* filepointer;
    filepointer = fopen(filename,"r+");
    if(filepointer == NULL){
        fclose(filepointer);
        printf("\nGREŠKA: Nije moguæe otvoriti podatak!");
    }else{
        printf("\nUspješno otvoren podatak.");
        printf("\nPoèinje konvertovanje.");
        proccess:
        if(c = fgetc(filepointer) != EOF){
            if(c == 0x9A){  // ZA Š
                c = 0x73;
                er = fputc( c , filepointer );
                if(er = EOF)
                    goto error;
            }else if(c == 0xF0){ //ZA Đ
                c = 0x64;
                er = fputc( c , filepointer );
                if(er = EOF)
                    goto error;
            }else if(c == 0x9E){
                c = 0x7A;
                er = fputc( c , filepointer );
                if(er = EOF)
                    goto error;
            }else if(c == 0xE8){
                c = 0x63;
                er = fputc( c , filepointer );
                if(er = EOF)
                    goto error;
            }else if(c == 0xE6){
                c = 0x63;
                er = fputc( c , filepointer );
                if(er = EOF)
                    goto error;
            }
            goto proccess;
        }
        if((c = fgetc(filepointer) ) != EOF){
            printf("Prijavljen EOF: Podatak se završava!");
            exit(0);
        }
    }
    return 0;
    error:
        printf("\nPrijavljen neoèekivani EOF,");
        printf("\nvjerovatno je neka greška!");
        return 1;
}

Platform: Windows 7 Home Premium (x64)
Compiler: Tiny C Compiler (x64)


Solution

  • Two things.

    Point 1. As per the C operator precedence, in your code

    if(c = fgetc(filepointer) != EOF)` 
    

    translates to

    if ( c =  (fgetc(filepointer) != EOF) )`
    

    which is not what you want. You should change that to

    if((c = fgetc(filepointer)) != EOF)`
    

    Point 2.

     if(er = EOF)
    

    = is the assignement operator. To compare values, you need to use eqality operator ==. Your code should be

    if(er == EOF)
    

    Note: If possible, please avoid goto. It is not a very good programming style. Try to make up a function and call that istead.