Pp9

Niew, lista słów zdania

Pp9
Dec 16th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct lista
  6. {
  7.     char napis[20];
  8.     struct lista *n;
  9. } element;
  10.  
  11. void wpisz(element **pocz, FILE *f);
  12. void czyt_l(element *pocz);
  13.  
  14. int main()
  15. {
  16.     FILE *f;
  17.     f=fopen("Dane.txt", "rt");
  18.  
  19.     element *slowa;
  20.     slowa=malloc(sizeof(element));
  21.     wpisz (&slowa, f);
  22.     fclose(f);
  23.     czyt_l(slowa);
  24.  
  25.  
  26.     return 0;
  27. }
  28.  
  29. void wpisz(element **slowo, FILE *f)
  30. {
  31.     char c, w[20];
  32.     element *pom=*slowo;
  33.     //fscanf(f, " %c", &c);
  34.     c=getc(f);
  35.     if(c==EOF)
  36.     {
  37.         *slowo=NULL;
  38.         return;
  39.     }
  40.     while(c!=EOF)
  41.     {
  42.  
  43.         while(c!='.')
  44.         {
  45.             int j;
  46.             for(j=0;c!=' '&&c!='.'&&c!='\0';j++)
  47.             {
  48.                 w[j]=c;
  49.                 //fscanf(f, " %c", &c);
  50.                 c=getc(f);
  51.             }
  52.             w[j]='\0';
  53.             strcpy(pom->napis, w);
  54.  
  55.         pom->n=malloc(sizeof(element));
  56.         pom=pom->n;
  57.         }
  58.     }
  59.     pom=NULL;
  60. }
  61.  
  62. void czyt_l(element *slowo)
  63. {
  64.     int i;
  65.     element *c;
  66.     c=slowo;
  67.     printf("\n");
  68.     if(c==NULL) printf("\nLista pusta!");
  69.     for(i=0; c!=NULL; i++,c=c->n) printf("%d slowo w liscie: %s\n",i+1, c->napis);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment