What flag could I use to stop the execution of the ler_disciplinas function?

Asked

Viewed 59 times

-3

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

typedef struct materias {

char disciplinas[20];
char semana[7][15];

}materias;

void ler_disciplinas ();

int main(){
materias m[100];

FILE* f;
f = fopen("/home/lucas/Área de Trabalho/prova2.txt","r");
ler_disciplinas(m,f);
fclose(f);

}
void ler_disciplinas (materias* m, FILE* f){

int i = 0,k = 0,j = 0;

for(;;){

    for(i = 0;;i++){
        (*m).disciplinas[i] = fgetc(f);
        if((*m).disciplinas[i] == ' '){break;}
    }
    printf("%s",(*m).disciplinas);

    for(j = 0;j<7;j++){
        for(k = 0;k<100;k++){
            (*m).semana[j][k] = fgetc(f);
            if((*m).semana[j][k] == ' ' || (*m).semana[j][k] == '\n'){break;}
        }
        printf("%s",(*m).semana[j]);
        if((*m).semana[j][k] == '\n'){break;}
    }   
}
}

This is what the program should read: matematica second fourth Friday' n' Portuguese terca quinta' n' biology second fourth' n' sixtieth geography

  • 1

    There is no flag to stop execution. There is the command break. I think it’s important to say that in Stack Overflow, you should always provide the content of the full text file as well as the problem statement. Also, there are several problems in your code (don’t give up!). You’re trying to solve but asked the question to what do you think the problem is and not the problem itself. Ask (almost) always about the error message of the code - that is Segmentation Failure in the case - with the code and everything relevant to run the program (in case, put the TXT).

1 answer

0

Well, I decided to fix your program so as to make it executable as a reference. You’ve had plenty of time to figure it out, and maybe you already have. Anyway, I tried to maintain your reasoning pattern with some improvements and modifications so that you understand as much as possible of it.

The important thing about this code is that it will serve as a reference for you to learn how to use ties better while, do-while and for which caused much of your headache - in evidence, the for. I really used all three to make him learn.

Look for more about looping in C. Also look about pointers, chains (arrays) of chain pointers and pointers (arrays). These are matters of paramount importance, much more if we consider that they are used in this program.

I sincerely recommend that you try one more time, look for the theory and try again and so on. Only then, if necessary, rely on the code set out here.

As usual, I put my version of this program. Again, I recommend that you look only after doing yours. It is also on Github.

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

#define ARQUIVO "/home/lucas/Área de Trabalho/prova2.txt"

#define EOL '\n'


typedef struct _materia { /* Evite ambos 'typedef' e struct compartilhar nome */

    char nome[20];          /* 'nome' é melhor - referencia a matéria */
    char dia_semana[7][15]; /* 'dia_semana' é melhor (auto-explicativo) */

} materias;


void ler_disciplinas ();


int main(){

    materias m[100];
    FILE *f;

    printf("\n");

    f = fopen( ARQUIVO, "r" );

    fseek( f, 0L, SEEK_END ); 

    if( ftell( f ) > 0 ){ /* Só chama 'ler_disciplinas()' se 'f' não é vazio */

        rewind( f );

        ler_disciplinas( m, f );

    }

    fclose( f );

    printf("\n");

    /* Sempre retorne algum valor se sua main tem algum tipo (i.e. 'int') */
    return 0;

}


void ler_disciplinas( materias *m, FILE *f ){

    /* Nesta função:
    * 
    * - não faça o laço 'for' sem argumentos pois é uma péssima prática;
    * - há uma série de problemas quanto ao uso dos índices i, j, k;
    * - há uma série de problemas quanto ao uso de laços;
    * - há uma série de problemas quanto ao uso de ponteiros;
    * 
    * ! Atente-se ao funcionamento de laços e ponteiros. Reescrevi quase tudo.
    * 
    */

    int i, k, j;
    char temp;

    for( i = 0; i < 100; i++ ){

        j = -1;

        do { /* Obtém nome da materia */

            ++j;

            temp = fgetc( f );

            if( temp == ' ' || temp == EOL ) { /* EOL == Fim de linha */

                m[i].nome[j] = '\0'; /* Fecha a string */

                printf( "\t%s", m[i].nome );

                j = -1;     /* Sai do laço DO (j) */

            } else if( temp == EOF ){  /* EOF == Fim de arquivo */

                j = -1;     /* Sai do laço DO (j) */
                i = 100;    /* Sai do laço FOR (i) */

            } else {

                m[i].nome[j] = temp;

            }

        } while( j != -1 );


        for(j = 0; j < 7; j++){ /* Índice do dia da semana */

            k = 0;

            while (k != -1 ){ /* Obtém o nome do dia da semana */

                temp = fgetc( f );

                if( temp == ' ' ) {

                    m[i].dia_semana[j][k] = '\0'; /* Fecha a string */

                    printf( " %s", m[i].dia_semana[j] );

                    k = -1;     /* Sai do laço WHILE (k) */

                } else if( temp == EOL ) { /* EOL == Fim de linha */

                    m[i].dia_semana[j][k] = '\0'; /* Fecha a string */

                    printf( " %s\n", m[i].dia_semana[j] );

                    k = -1;     /* Sai do laço WHILE (k) */
                    j = 7;      /* Sai do laço FOR (j) */

                } else if( temp == EOF ){ /* EOF == Fim de arquivo */

                    k = -1;     /* Sai do laço WHILE (k) */
                    j = 7;      /* Sai do laço FOR (j) */
                    i = 100;    /* Sai do laço FOR (i) */

                } else {

                    m[i].dia_semana[j][k] = temp;

                    k++;

                }

            }

        }

    }

}

Browser other questions tagged

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