Running gives me an error and I can not identify

0

I run my program and it goes well until I get to "l[strlen(l) - 1] = l[strlen(l)]; while (fgets(ll, sizeof (ll), "EventosDesportivosFutebol.txt") != NULL)" , (by debug I was able to find where it crashed), but I'm 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);

Here is the error:

Could someone help me?

So my code is this:

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, except that when you write to the file (eg sporting - benfica), you only register sporting. And I looked for the net some help and I found that of comparing strings but I am not able to frame and so to give error, can you help me pf?

    
asked by anonymous 27.12.2016 / 17:20

1 answer

3

The fgets function needs a "file pointer" (FILE *) to read a file. To get a file pointer you must "open" the file, with the function fopen . The fclose function "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);
    
27.12.2016 / 17:37