Skip to content

Instantly share code, notes, and snippets.

@Helias
Created April 14, 2018 21:17
Show Gist options
  • Save Helias/f66190d106fb5610fb991ade9b5e345c to your computer and use it in GitHub Desktop.
Save Helias/f66190d106fb5610fb991ade9b5e345c to your computer and use it in GitHub Desktop.
tmp gist
/*Homework 5*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//-----------------------------------------
typedef struct s {
char nome[20];
char numero[12];
}t_info;
typedef struct n1 {
t_info info;
struct n1 *next;
}t_lista,*plista;
//------------------------------------------
//quesito 1
int menu();
//quesito 2
int carica_lista(char nomefile[20], plista list);
//quesito 3
void stampa_lista(plista lista);
//--------------------------------------------
int main(void) {
t_lista list;
// list = NULL;
FILE *fp;
int scelta;
int x;
char nomefile[20];
do {
scelta = menu();
switch (scelta) {
case 1: {
x = carica_lista(nomefile, &list);
printf("Elementi caricati:%d\n", x);
break;
}
case 2: {
stampa_lista(&list);
break;
}
}
} while (scelta != 0);
return 0;
}
//-------------------------------------------
int carica_lista(char nomefile[20], plista list) {
FILE *fp;
t_info info;
int i = 0;
printf("Inserire il nome del file: ");
scanf("%s", nomefile);
// strcpy(nomefile, "prova.txt");
fp = fopen(nomefile, "r");
if (fp == NULL) {
printf("Impossibile aprire il file.\n");
return 0;
}
else {
while (fscanf(fp, "%s %s", list->info.nome, list->info.numero) == 2) {
// creo una nuova lista e la associo a list->next, poi con list punto a list->next per passare alla mia nuova lista
plista aux;
aux = malloc(sizeof(t_lista));
list->next = aux;
list = list->next;
i++;
}
}
return i;
}
void stampa_lista(plista lista) {
if (lista != NULL) {
printf("%s %s\n", (*lista).info.numero, (*lista).info.nome);
stampa_lista(lista->next);
}
if (lista == NULL) return;
}
int menu() {
int x;
printf("--------------------\n");
printf("1)Carica lista.\n");
printf("2)Stampa lista.\n");
printf("--------------------\n");
scanf("%d", &x);
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment