Kimossab

Erro carregar disciplinas

May 29th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. //---------------------LISTA DISCIPLINAS--------------------
  2. typedef struct disciplina
  3. {
  4.     Texto nome;
  5.     LAlunos *la;
  6.     LPerguntas *lp;
  7. }Disciplina;
  8.  
  9. typedef struct ndisciplina
  10. {
  11.     Disciplina *Info;
  12.     struct ndisciplina *prox;
  13. }NDisciplina;
  14.  
  15. typedef struct ldisciplinas
  16. {
  17.     NDisciplina *Inicio;
  18.     int Nel;
  19. }LDisciplinas;
  20.  
  21. //........
  22.  
  23. Disciplina *InicDisp(LDisciplinas *LD)
  24. {
  25.     Texto t = (Texto)malloc(sizeof(char));
  26.     int n;
  27.  
  28.     printf("\nIntroduza o nome da disciplina:");
  29.     fflush(stdin);
  30.     gets(t);
  31.    
  32.     NDisciplina *ND = LD->Inicio, *Aux = NULL;
  33.     bool test = false;
  34.  
  35.     for(int i = 0; i<LD->Nel; i++)
  36.     {
  37.         if(stricmp(ND->Info->nome, t) == 0)
  38.         {
  39.             test = true;
  40.             break;
  41.         }
  42.         Aux = ND;
  43.         ND = ND->prox;
  44.     }
  45.     if(test) return ND->Info;
  46.  
  47.     printf("\nIntroduziu como nome da disciplina \"%s\" que não existe ainda.\nDeseja criar esta disciplina?", t);
  48.     printf("\n1 = Sim | 2 = Não\n");
  49.     while(scanf("%d", &n) != 1 || (n < 1 || n > 2))
  50.     {
  51.         while((getchar()) != '\n');
  52.         printf("\nApenas 1 ou 2!\n");
  53.         printf("\nIntroduziu como nome da disciplina \"%s\" que não existe ainda.\nDeseja criar esta disciplina?", t);
  54.         printf("\n1 = Sim | 2 = Não\n");
  55.     }
  56.    
  57.     if(n == 1)
  58.     {
  59.         if(!Aux)
  60.         {
  61.             LD->Inicio = CND();
  62.             LD->Nel = 1;
  63.             ND = LD->Inicio;
  64.             ND->Info = ID();
  65.             sprintf(LD->Inicio->Info->nome, "%s", t);
  66.             ND->Info->la = CLA();
  67.             ND->Info->lp = CLP();
  68.         }
  69.         else
  70.         {
  71.             LD->Nel++;
  72.             Aux->prox = CND();
  73.             ND = Aux->prox;
  74.             ND->Info = ID();
  75.             sprintf(ND->Info->nome, "%s", t);
  76.             ND->Info->la = CLA();
  77.             ND->Info->lp = CLP();
  78.         }
  79.         GuardarNomeDiciplinas(LD);
  80.         return ND->Info;
  81.     }
  82.     else
  83.         return InicDisp(LD);
  84.     //SaveSubjects(LD);
  85. }
  86.  
  87. //..................
  88.  
  89. void CarregarNomeDisciplinas(LDisciplinas *LD)
  90. {
  91.     if(!fexist(DIS_PATH))
  92.         LD->Nel = 0;
  93.     else
  94.     {
  95.         FILE *f = fopen(DIS_PATH, "r");
  96.         Texto t = (Texto)malloc(MAX_TEXTO);
  97.         fgets(t, MAX_TEXTO, f);
  98.         sscanf(t, "%d", &LD->Nel);
  99.  
  100.         LD->Inicio = CND();
  101.         NDisciplina *ND = LD->Inicio;
  102.  
  103.         for(int i=0; i < LD->Nel; i++)
  104.         {
  105.             ND->Info = ID();
  106.             fgets(t, MAX_TEXTO, f);
  107.             ND->Info->nome = (Texto)realloc(t, strlen(t)+1);
  108.             ND->prox = CND();
  109.             ND = ND->prox;
  110.         }
  111.     }
  112. }
  113.  
  114. void GuardarNomeDiciplinas(LDisciplinas *LD)
  115. {
  116.     FILE *f = fopen(DIS_PATH, "w");
  117.     fprintf(f,"%d", LD->Nel);
  118.     NDisciplina *D = LD->Inicio;
  119.     while(D)
  120.     {
  121.         fprintf(f, "\n%s",  D->Info->nome);
  122.         D = D->prox;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment