Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /* Mateusz Buława */
  2.  
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #define MAX 20
  8.  
  9. struct pilkarz {
  10. char nazwisko[40];
  11. char klub[40];
  12. float zarobki;
  13. struct pilkarz *next;
  14. };
  15.  
  16. int main() {
  17.  
  18. FILE * A;
  19. char wejscie[MAX];
  20.  
  21. //otwarcie pliku
  22. printf("Podaj nazwe pliku z rozszerzeniem .txt: ");
  23. scanf("%s", &wejscie);
  24. A = fopen(wejscie, "r");
  25.  
  26. //blad otwarcia pliku
  27. if (A == NULL) {
  28. printf("\nBlad otwarcia pliku.\n");
  29. fclose(A);
  30. return 0;
  31. }
  32. else
  33. printf("Plik wejsciowy zostal otwarty.\n");
  34.  
  35. struct pilkarz * pel = NULL; //pierwszy element
  36. struct pilkarz * pom=NULL; //wskaznik na blok listy
  37.  
  38. //tworzenie listy
  39. while (feof(A) == 0) {
  40. if (pel = NULL) {
  41. pel = pom = (struct pilkarz*)malloc(sizeof(struct pilkarz));
  42. }
  43. else {
  44. pom->next = (struct pilkarz*)malloc(sizeof(struct pilkarz));
  45. pom = pom->next;
  46. }
  47. //wczytanie z pliku do listy
  48. fscanf(A, "%s %s %f", &pom->nazwisko, &pom->klub, &pom->zarobki);
  49. //wypisanie w konsoli
  50. printf("Nazwisko pilkarza: %s, klub: %s, pensja: %.2f$", pom->nazwisko, pom->klub, pom->zarobki);
  51. pom->next = NULL;
  52. }
  53. //koniec programu
  54. fclose(A);
  55. pom = pel;
  56. while (pom != NULL) {
  57. pel = pom;
  58. pom = pel->next;
  59. free(pel);
  60. }
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement