How to compare characters from an external file to the ASCII table in C?

Asked

Viewed 336 times

1

I need to make a program in C that reads an external file with characters inside and checks if the character code is between the codes >32 and <126 of the ASCII table. If it is between code 32 and 126 I return "Contains in table"+character If you are not between this code space I return "Does not contain in table"+character. I did it this way, but "Else" is returning different characters than I type.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  FILE *pont_arq;

  int c=0;
  char palavra[20];

  pont_arq = fopen("arquivo.txt", "rt");

do
{
    c = getc(pont_arq);

    if (c >= 32 && c <= 126){
        printf(" | %c %i contem na tabela", c, c);
    }else
    {
        printf("\n\n %i nao contem na tabela ", c, c);
    }

} while (c != EOF);

  fclose(pont_arq);

return(0);
}
  • I understand the opening mode r, r+ and rb, now what is the opening mode rt used there by fopen?

  • 1

    @Jeffersonquesado some implementations consider characters different from standard ones. In this case, the "t" would symbolize a text file (which obviously must exist). In IBM z/OS this feature is available.

  • Show an example, preferably small, of a chunk of the input, and also of the output, that contains "different characters than the type". Note that in the Else printf you only enter the format for a variable but in the function you enter two variables.

  • The entry put "hjç" (no quotes), in this case the output was: "h 104 contains in table | j 106 contains in table | 195 does not contain in table | 167 does not contain in table | -1 does not contain in table". But the output should have been only "h 104 contains in table | j 106 contains in table | ç 135 does not contain in table".

  • The standard ASCII table defines only characters from 0 to 127. It does not include accented and special characters in your case ç. Maybe you should work with UTF-8 character encoding (preferably) or Latin1 (ISO/IEC 8859-1) to handle characters not represented in the ASCII table.

1 answer

1

The problem is that you read the end of file (EOF), test whether the character is inside the table or not, and only then test you should finish the loop (c != EOF). It would make no sense to test anything else if you’ve reached the end of the file.

When you read the EOF, which means end of file, you should not process anything else, you should leave immediately as you have already reached the end of file. By chance, the EOF is worth -1, which is why you are receiving this output.

Just put the test in your code as follows:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  FILE *pont_arq;

  int c=0;
  char palavra[20];

  pont_arq = fopen("arquivo.txt", "rt");

  do
  {
    c = getc(pont_arq);

    if(c != EOF) {
      if (c >= 32 && c <= 126){
          printf(" | %c %i contem na tabela", c, c);
      } else {
          printf("\n\n %i nao contem na tabela ", c, c);
      }
    }
  } while (c != EOF);

  fclose(pont_arq);

  return(0);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.