File handling - HELP

Asked

Viewed 23 times

0

I’m trying to develop a code whose goal is to read an existing text file and ask the user for a carectere, then the program should return to the user how many times the typed carectere repeats in the read text file. However, the program only returns me the amount of letters existing in the file and I can not solve at all. If anyone can give me a strength, I appreciate it. I need to resolve urgently.

Follow the code below:

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

int main()
{
    setlocale(LC_ALL, "Portuguese");
    
    FILE *arquivo;
    char letra;
    int total = 0;
  int i, c;
    
    printf ("Digite um caracter:");
    scanf ("%c", &letra);
    
    if ((arquivo=fopen("conto.txt", "r")) != NULL)
    {   
        while((letra=fgetc(arquivo)) != EOF)
        {
            if(letra || letra == toupper(letra)) total++;
       int getc (FILE *fp);
        }
    }

    fclose(arquivo);
    
    printf ("Existem %i letras '%i' no arquivo.\n", total, letra);
}
  • 1

    if(letra || letra == toupper(letra)), the first term of the condition, if (letra), will be true for any letter, so it brings the total of letters in the file. You also read the user input in the variable letra, but then uses the same variable to interact over the file. You basically overwritten the user input and lost information. There is also a code line, int getc (FILE *fp);, that makes no sense at all.

  • Could you tell me how you could fix it then? I’m a beginner and almost don’t know to be honest.

  • Start by describing the solution you want to implement in pure Portuguese. Writing code without knowing which code to write is inefficient in 99.9% of cases. It seems to me that you tried to write the solution in C without even knowing what the solution was you wanted to do and it negatively impacted your logic. So much so that you have code that you wrote yourself and you didn’t know how to interpret or justify why you wrote it. This should never happen.

  • Yes, I agree with you. But it is precisely because I have these doubts that I have come to this site. Because since I don’t understand what I did wrong, I look for someone who knows more about the subject to help me, and so I don’t make the same mistake again

  • Can you describe to us, in text, what is the solution you would like to implement?

  • Then, the program should read the text file (conto.txt) and ask the user to type a carectere. Then the program should find how many times the carectere typed by the user repeats itself in the text file. However, the program is only returning me the total value of characters that the text file has. To be honest, I’m stuck in this and I can’t seem to solve it

Show 1 more comment

1 answer

-1

From your description what you want is:

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

int main()
{
    setlocale(LC_ALL, "Portuguese");
    
    FILE *arquivo;
    char letra;
    int total = 0;
   int c;
    
    printf ("Digite um caracter:");
    scanf ("%c", &letra);
    
    if ((arquivo=fopen("conto.txt", "r")) != NULL)
    {   
        while((c = fgetc(arquivo)) != EOF)
        {
            if(letra == c) total++;
        }
    }

    fclose(arquivo);
    
    printf ("Existem %i letras '%c' no arquivo.\n", total, letra);
}

If you don’t want to differentiate between upper and lower case, then use:

f(toupper(c) == toupper(letra)) total++;
  • Man, that’s exactly it... Thank you so much, really. You don’t know how much it helped me. Thanks a lot, buddy!!!

Browser other questions tagged

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