Hyluss

wczytywanie z pliku, zapis do pliku, struktura

Jan 15th, 2017
595
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. //wczytywanie z pliku, zapis do pliku, struktura
  2.  
  3. // ciezary.cpp : Defines the entry point for the console application.
  4. //
  5.  
  6. #include "stdafx.h"
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #define _CRT_SECURE_NO_DEPRECATE
  12.  
  13.  
  14.  
  15. float Waga[20];
  16. char Imie [20][20];
  17. int Powt[20];
  18. float Srednia[20];
  19.  
  20.  
  21. int _tmain(int argc, _TCHAR* argv[])
  22. {
  23.     FILE *wczytaj;
  24.     FILE *zapisz;
  25.    
  26. int  i=0;
  27. int porownanie=0;
  28.  
  29.     wczytaj = fopen ("wciezary.txt","r");
  30.     if (wczytaj == NULL) {
  31.     perror ("blad");
  32.     exit(1);
  33.     }
  34.  
  35. while (!feof (wczytaj))
  36. {
  37.     fscanf (wczytaj,"%s %f", &Imie[i], &Waga[i]);
  38.     Powt[i] = 1;
  39.     printf ("%s %.2f \n", Imie[i], Waga[i]);
  40.     for (int j=0; j<i;j++)
  41.     {
  42.         porownanie = strcmp (Imie[i], Imie[j]);
  43.    
  44.         if (0 == porownanie)
  45.         {
  46.             Waga[j] = Waga[i] + Waga[j];
  47.             Powt[j]++;
  48.             i--;
  49.         }
  50.     }
  51.     i++;
  52. }
  53. zapisz = fopen ("zciezary.txt","w");
  54. if (zapisz == NULL) {
  55. perror ("blad");
  56. fclose (wczytaj);
  57. exit (1);
  58. }
  59.  
  60.  
  61. for (int j=0; j<i;j++) {
  62. Srednia[j] = Waga[j] / Powt[j];
  63. printf ("%s podniosl srednio %.2f \n", Imie[j],Srednia[j]);
  64. fprintf (zapisz,"%s podniosl srednio %.2f \n", Imie[j],Srednia[j]);
  65. }
  66.     _getch();
  67.     return 0;
  68. }
  69.  
  70. /* tak wyglada "wciezary.txt"
  71. Adam 10
  72. Adam 5
  73. Lukas 14
  74. Marcin 99
  75. Adam 7
  76. Marcin 53
  77. Wiktor 51
  78. Darek 3
  79. Wiktor 4
  80. Darek 5
  81. Darek 6
  82. */
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment