-3

i tried this code to read each character in text file and if the chracter =ا print @

else print the character

void
preprocess_file (FILE *fp)

{
  char d;

    for (;;)
      { 
    d = getc (fp);
    if (d == EOF)
        break;
        if (d =='ا')
        printf ("@\n");
        else
    printf ("%c\n ", d);
      }
}

int
main(int argc, char *argv [])
{
    preprocess_file (stdin);

    exit (0);
}

but the output file is showing strange result as following

    ï
 »
 ؟
 ط
 §
 ظ
 „
 ظ
 …
 ط
 ¤

any help?

user1200219
  • 179
  • 1
  • 4
  • 2
    What's wrong with the code? Indentation, for starters. – nhgrif Nov 05 '13 at 23:09
  • 1
    1. You have `cc` in one place, and `d` in another. – librik Nov 05 '13 at 23:10
  • 2
    This question appears to be off-topic because it is about a trivial problem that has already been discussed numerous times and the solution wouldn't have required anything but actually paying attention in class. –  Nov 05 '13 at 23:12
  • You are probably running into codeset and wide character input issues; the `'ا'` character has a Unicode value U+0267 which doesn't fit into a single byte. – Jonathan Leffler Nov 05 '13 at 23:45

4 Answers4

1

your problem is here

if (d ='ا')

you're assigning 'ا' to d

you should ==

if (d =='ا')
  • 1
    `d` being a `char` is fine to compare to another `char`, but not to a `char*`, which is what`"|"` is. You need to replace the double-quotes with single-quotes. – Zac Howland Nov 05 '13 at 23:21
1

if (d ="ا")

For a comparison you want == instead of =, and a single character instead of a string literal, like d == 'ا'.

As-is, it's attempting to assign the address of a string literal (which must be non-null) to a char, then checking whether the result is non-zero. While it's possible that the conversion from pointer to char could produce either 0 or some non-0 value, it's probably going to produce a consistent value, so at least with a typical implementation, that code will always execute.

You also have a problem because you've defined d to be a char instead of an int. To do the comparison to EOF correctly, you really need to make it an int.

If I were doing this, I think I'd do it a bit differently--something on this general order:

int ch;

while ((ch=getc()) != EOF) // or: `while (EOF != (ch=getc()))`
   printf("%c\n", ch == 'ا' ? '@' : ch);
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1
char d;
//...
if (d ="ا")

d is a char and you are assigning it to a character array of size 2. So you are squashing memory (for one), and your comparison will always be non-zero, so your condition will always be true and you will always run the block under your if-condition. What you wanted was

char d;

for (;;)
{ 
    d = getc (fp); // should be d, not cc
    if (d == EOF)
        break;

    if (d == 'ا') // note the SINGLE quotes
        printf ("@\n");
    else
        printf ("%c\n ", d);
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • 1
    I didn't notice it at first either (haven't done C-style I/O in a while), but to compare to EOF correctly, you want to change `d` to an `int` instead of a `char`. – Jerry Coffin Nov 05 '13 at 23:30
  • @JerryCoffin Technically, yes. Though, since EOF = -1, it *should* be okay to just do the `d == EOF` check. Granted, `(int)d == EOF` would be more accurate - but that has always bugged me about the C character functions (why they decided to use `int` to manipulate `char` is beyond me). – Zac Howland Nov 06 '13 at 02:50
  • @ZacHowland: d==EOF is *not* all right. The problem is that you can have a 255 in the input, which will get converted to a -1 as a char, but is *not* the same as a real EOF (and casting to an `int` won't help either). They used `int` so that EOF could/would be different from any value that could be read from the file. – Jerry Coffin Nov 06 '13 at 03:07
  • But 255 is not a valid value for a `char` (`unsigned char`, yes, `char`, no). – Zac Howland Nov 06 '13 at 03:10
0

You should write: if (d == 'ا')

It's == and not = and it's '|' for the char otherwise it's a string.

Also, change cc = getc (fp); by d = getc(fp). I don't know what's cc.

You may have some problems with that character, in particular if you save your file in UTF-8. But dealing with multiple bytes characters is difficult, in particular for a beginner. Have a look to that answer: https://stackoverflow.com/a/1373520/1787973

Community
  • 1
  • 1
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • i did but it showed strange character in output and when make the program it shows gcc -g -O -Wall -ansi -pedantic ascii1.c -o ascii1 ascii1.c: In function 'preprocess_file': ascii1.c:19:12: warning: multi-character character constant – user1200219 Nov 05 '13 at 23:21
  • @user1200219 You character ا is not an ascii character and can't be held in a char. You probably need unicode support and I don't think you want to get into that at the moment. No offense. – François Moisan Nov 05 '13 at 23:42
  • Save your file in something that is not UTF-8. For instance ISO-8859-15. – Maxime Chéramy Nov 05 '13 at 23:43