Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define CRT_SECURE_NO_WARNINGS
  5.  
  6.  
  7. struct kontakt
  8. {
  9.     char *imie;
  10.     char *nazwisko;
  11.     char *grupa;
  12.     char *numer;
  13.    
  14. };
  15.  
  16. struct lista
  17. {
  18.     struct lista *poprzedni;
  19.     struct lista *nastepny;
  20.     struct kontakt dane;
  21. }*top;
  22.  
  23.  
  24. struct lista *  wczytaj(FILE *plik)
  25. {
  26.     char buff[100];
  27.     struct lista *ptr;
  28.     ptr= malloc(sizeof(struct lista));
  29.     fscanf(plik, "%[^;];", buff);
  30.     ptr->dane.imie=(char*)malloc(sizeof(char)* (strlen(buff) + 1));
  31.     strcpy (ptr->dane.imie ,buff);
  32.     fscanf(plik, "%[^;];", buff);
  33.     ptr->dane.nazwisko=(char*)malloc(sizeof(char)* (strlen(buff) + 1));
  34.     strcpy (ptr->dane.nazwisko ,buff);
  35.     fscanf(plik, "%[^;];", buff);
  36.     ptr->dane.grupa=(char*)malloc(sizeof(char)* (strlen(buff) + 1));
  37.     strcpy (ptr->dane.grupa ,buff);
  38.     fscanf(plik, "%[^;];\n", buff);
  39.     ptr->dane.numer=(char*)malloc(sizeof(char)* (strlen(buff) + 1));
  40.     strcpy (ptr->dane.numer ,buff);
  41.     printf("%s; %s; %s; %s;\n", ptr->dane.imie, ptr->dane.nazwisko, ptr->dane.grupa, ptr->dane.numer);
  42.     return ptr;
  43.  
  44. }
  45.  
  46.  
  47.  
  48. struct lista *tworz()
  49. {
  50.     struct lista *ptrp, *ptr;
  51.     FILE *f;
  52.     char *c;
  53.     ptr= malloc(sizeof(struct lista));
  54.     ptrp= malloc(sizeof(struct lista));
  55.     f= fopen("lista.txt", "r");
  56.     while(c[0]!=EOF)
  57.     {
  58.         ptr = wczytaj(f);
  59.         c[0]=getc(f);
  60.         ptr->dane.imie=strcpy(c, ptr->dane.imie);
  61.         if(ptr->poprzedni!=NULL)
  62.         {
  63.             ptr->poprzedni->nastepny = ptr;
  64.         }
  65.         ptr->poprzedni= ptrp;
  66.         ptrp= ptr;
  67.     }
  68.     fclose(f);
  69.     return ptr;
  70.  
  71. }
  72.  
  73. void printlista(struct lista *glowa)
  74. {
  75.     while(glowa->poprzedni!=NULL)
  76.     {
  77.         printf("%s; %s; %s; %s;\n", glowa->dane.imie, glowa->dane.nazwisko, glowa->dane.grupa, glowa->dane.numer);
  78.         glowa=glowa->poprzedni;
  79.     }
  80. }
  81.  
  82. int main()
  83. {
  84.     top= tworz();
  85.     printlista(top);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement