Running gives me a mistake and I can’t identify

Asked

Viewed 77 times

0

I run my program and it’s all right until I get to "l[strlen(l) - 1] = l[strlen(l)]; while (fgets(ll, sizeof (ll), "EventosDesportivosFutebol.txt") != NULL)", (doing the debug I was able to find out where I crashed), but I am not able to solve the problem, below is the code:

while (camp<5) {
    printf("Altere Jogos da Liga dos Campeões %d: ", camp+1);
    scanf("%s", Jogos[camp].campeoes);
    while (getchar() != '\n');
    camp++;

    char l[20] = "Liga dos Campeões";
    char ll[20] = {l};
    fgets(l, sizeof(l), stdin);
    l[strlen(l) - 1] = l[strlen(l)];

while (fgets(ll, sizeof (ll), "EventosDesportivosFutebol.txt") != NULL)
    if (strstr(ll, l) != NULL)
    {
        memset(l, NULL, 1000);
    }
    fclose("EventosDesportivosFutebol.txt");

feventosDesportivosFutebol = fopen("EventosDesportivosFutebol.txt", "w");
                            if (feventosDesportivosFutebol != NULL) {
                                fprintf(feventosDesportivosFutebol, "\n\nLiga dos Campeões:\n\n");
                                for (camp = 0; camp<5; camp++) {
                                    fprintf(feventosDesportivosFutebol, "%s\n", Jogos[camp].campeoes);
                                }
                            }
                            fclose(feventosDesportivosFutebol);

and here the error:
inserir a descrição da imagem aqui

Someone could help me?

So this is my code:

while (camp<5) {
                        printf("Altere Jogos da Liga dos Campeões %d: ", camp+1);
                        scanf("%s", futebol[camp].campeoes);
                        while (getchar() != '\n');
                        camp++;
                    }
                    feventosDesportivosFutebol = fopen("EventosDesportivosFutebol.txt", "w");
                    if (feventosDesportivosFutebol != NULL) {
                        fprintf(feventosDesportivosFutebol, "\n\nLiga dos Campeões:\n\n");
                        for (camp = 0; camp<5; camp++) {
                            fprintf(feventosDesportivosFutebol, "%s\n", futebol[camp].campeoes);
                        }
                    }
                    fclose(feventosDesportivosFutebol);

Here the code works well, but when you scroll to the file (e.g. Sporting - Benfica), only Sporting is recorded. And I searched the net for some help and I found that comparing strings but I am not able to frame and so give error, will help me?

1 answer

3

The function fgets needs a "file Pointer" (FILE*) to read a file.
To get a Pointer file you need to "open" the file, with the function fopen.
The function fclose "closes" the file referenced by a "file Pointer".

FILE* fp = fopen("EventosDesportivosFutebol.txt", "r");
if (fp == NULL)
{
   // erro...
}

// while (fgets(ll, sizeof (ll), "EventosDesportivosFutebol.txt") != NULL) // ERRADO!!!
while (fgets(ll, sizeof (ll), fp) != NULL)
{
   // ...
}

// fclose("EventosDesportivosFutebol.txt"); // ERRADO!!!
fclose(fp);
  • 1

    Joseph could give a brief explanation of the code?

  • I’m sorry I was missing the rest of the code, I could review what’s wrong in all the code pf josé?

  • @ricascross: your code still has an error in "fgets" and the first "fclose", see the example I wrote..., but complete, which can be compiled and tested to facilitate understanding (yours and the readers of the OS)

  • I’ll edit and doubt!

Browser other questions tagged

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