ho provato a seguire il codice che mi hai proposto, ma non ha avuto successo......
quindi per cercare di arrivare ad un punto comune, ti mando l'intero codice che sto elaborando....... chiaramente manca la lettura e scrittura dal file di testo al file binario!!!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h> //per la funzione sleep()
struct studente
{
char cognome[15];
char nome [15];
int voto;
struct studente *next;
};
struct studente_suff
{
char nome[15];
char cognome[15];
int voto;
struct studente_suff *prox;
};
int inserimento_lista (struct studente **, FILE *);
void visualizza_lista (struct studente *, FILE *);
int trova_elem_suff (struct studente **, struct studente_suff **, int);
void visualizza_lista2 (struct studente_suff *, FILE *);
void main()
{
struct studente *head = NULL;
struct studente_suff *head_newlist = NULL;
FILE *fpt, *fpt_1;
FILE *fpb; //questo è il puntatore a file binario che non ho ancora utilizzato
int cont = 0;
int cont_suff = 0;
fpt = fopen ("C:\\Programmi\\Microsoft Visual Studio\\MyProjects\\programma_laboratorio3\\archiv io.txt", "r");
if (fpt == NULL)
printf ("Il file che si tenta di aprire e' inesistente!!!\n");
else
{
rewind (fpt);
cont = inserimento_lista (&head, fpt);
printf ("All'interno della lista sono stati inseriti %d studenti\n\n", cont);
fpt_1 = fopen ("C:\\Programmi\\Microsoft Visual Studio\\MyProjects\\programma_laboratorio3\\alunni .txt", "w");
if (fpt_1 == NULL)
printf ("Il file non e' presente sul disco!!\n");
else
{
visualizza_lista (head, fpt_1); //NO &head perchè non voglio modificare la lista ma visualizzarla
cont_suff = trova_elem_suff (&head, &head_newlist, cont);
printf ("Sono risultati sufficenti %d studente/i\n\n", cont_suff);
fpt_1 = fopen ("C:\\Programmi\\Microsoft Visual Studio\\MyProjects\\programma_laboratorio3\\alunni .txt", "a");
if (fpt_1 == NULL)
printf ("Il file non e' presente sul disco!!\n");
else
{
fprintf(fpt_1, "\n\n -------- LISTA studenti sufficenti ----------\n");
visualizza_lista2 (head_newlist, fpt_1);
}
}
}
}
int inserimento_lista(struct studente **testa, FILE *A)
{
char name[15];
char surname[15];
int mark;
int cont = 0;
struct studente *p, *nuovo;
while (fscanf(A, "%s%s%d", name, surname, &mark) != EOF)
{
nuovo = malloc (sizeof(struct studente));
p = *testa;
*testa = nuovo;
nuovo->next = p;
strcpy(nuovo->nome, name);
strcpy(nuovo->cognome, surname);
nuovo->voto = mark;
cont = cont + 1;
}
printf ("Elementi inseriti nella LISTA!!!!\n\n");
return cont;
}
void visualizza_lista (struct studente *testa, FILE *A)
{
struct studente *punt;
punt = testa;
fprintf(A, " --------- LISTA ----------\n");
fprintf(A, "\nTESTA ");
if (punt == NULL)
fprintf(A, " --> NULL");
else
do
{
fprintf(A, "--> %s %s %d ", punt->nome, punt->cognome, punt->voto);
punt = punt->next;
} while (punt!=NULL);
fprintf(A, " --> NULL");
}
int trova_elem_suff (struct studente **testa, struct studente_suff **testa_nuovalista, int num_elementi)
{
struct studente *punt;
struct studente_suff *nuovo;
struct studente_suff *p;
int i;
int cont = 0;
punt = *testa;
printf ("Lettura LISTA in corso...... attendere qualche secondo\n");
Sleep (2000);
for (i=1; i <= num_elementi; i++)
{
if (punt->voto >= 18)
{
nuovo = malloc (sizeof(struct studente_suff));
p = *testa_nuovalista;
*testa_nuovalista = nuovo;
nuovo->prox = p;
strcpy (nuovo->nome, punt->nome);
strcpy (nuovo->cognome, punt->cognome);
nuovo->voto = punt->voto;
cont = cont + 1;
printf ("--------Studente Sufficente--------\n");
printf ("Nome: %s\n", nuovo->nome);
printf ("Cognome: %s\n", nuovo->cognome);
printf ("Voto: %d\n\n", nuovo->voto);
}
punt = punt->next;
}
if (cont == 0)
{
return 0;
}
else
{
return cont;
}
}
void visualizza_lista2 (struct studente_suff *testa_nuovalista, FILE *A)
{
struct studente_suff *punt;
punt = testa_nuovalista;
fprintf(A, "\nTESTA ");
if (punt == NULL)
fprintf(A, " --> NULL");
else
do
{
fprintf(A, "--> %s %s %d ", punt->nome, punt->cognome, punt->voto);
punt = punt->prox;
} while (punt!=NULL);
fprintf(A, " --> NULL");
}