Advertisement
Guest User

Untitled

a guest
May 4th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. #include  <stdio.h>
  2. #include <stdlib.h>
  3. //Se non erro bisognava creare una lista di nodi, salvarla su file, e poi creare una funzione che preleva la lista dal file, con i dovuti controlli sul campo nodo.next xche essendo un indirizzo alla riapertura del programma il nodo non sta più in quel vecchio indirizzo e quindi bisognava riassegnargli un nuovo indirizzo
  4. struct lista
  5. {
  6. char numero;
  7. struct lista *prossimo;
  8. };
  9. struct lista *creanodo(struct lista *testa,char x);
  10. int main (void)
  11. {
  12. char x=0;
  13. struct lista *testa;
  14. testa=NULL;
  15. printf("Inserisci elemento (a capo per terminare)\n");
  16. for(;;)
  17. {
  18. scanf("%c",&x);
  19. if(x=='\n')
  20. break;
  21. testa=creanodo(testa,x);
  22. }
  23. FILE *input, *output;
  24. input=fopen("lista1.txt","w");
  25. if(input==NULL)
  26. {
  27. printf("Errore\n");
  28. return 0 ;
  29. }
  30. for(testa;testa!=NULL;testa=(*testa).prossimo)
  31. {
  32.  
  33. putc((*testa).numero,input);
  34.  
  35. }
  36.  
  37. fclose(input);
  38. input=fopen("lista1.txt","r");
  39. output=fopen("lista2.txt","w");
  40. if(input==NULL||output==NULL)
  41. {
  42. printf("Errore\n");
  43. return 0 ;
  44. }
  45. while((x=getc(input))!=EOF)
  46. {
  47. putc(x,output);
  48. }
  49. fclose(input);
  50. fclose(output);
  51. }
  52.  
  53. struct lista *creanodo(struct lista *testa,char x)
  54. {
  55. struct lista *nuovo;
  56. nuovo=malloc(sizeof(struct lista));
  57. (*nuovo).numero=x;
  58. (*nuovo).prossimo=testa;
  59. return nuovo;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement