nik123
nik123

Reputation: 113

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)

Upvotes: 0

Views: 64

Answers (2)

Paul R
Paul R

Reputation: 212929

You're getting bitten by operator precedence:

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

should be:

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

Note that if you had compiled with warnings enabled (e.g. gcc -Wall ...) the compiler would have helpfully pointed out this mistake:

main.c:19:41: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if(c = fgetc(filepointer) != EOF){

You would also have been warned about another mistake:

main.c:23:28: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
                 if(er = EOF)

This line should of course be:

                 if(er == EOF)


A further problem - you're trying to modify the input file "in place" - this is generally a bad idea (any errors typically mean you are left with a corrupted original file). Furthermore, the way the code is currently written it won't do what you want (as you have seen). A better approach would be to write the output to a new file, and then on successful completion you can delete the old file and rename the new file.
As a bonus, some pointers on programming style:

  • avoid globals
  • don't use goto

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

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.

Upvotes: 0

Related Questions