peterzig

[PO] Struktury Auto w C

Feb 29th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. // struktury.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <stdlib.h>
  5. #include "conio.h"
  6. #include <stdio.h>
  7.  
  8. struct Samochod
  9. {
  10.     char marka[20];
  11.     char kolor[20];
  12.     int rocznik;
  13. };
  14.  
  15.  
  16. int main()
  17. {
  18.     Samochod *sam;
  19.     sam = (Samochod*)malloc(3 * sizeof(Samochod));
  20.     for (int i = 0; i < 3; i++)
  21.     {
  22.         printf("Marka: ");
  23.         scanf("%s", &sam[i].marka);
  24.         printf("Kolor: ");
  25.         scanf("%s",&sam[i].kolor);
  26.         printf("Rocznik: ");
  27.         scanf("%d",&sam[i].rocznik);
  28.     }
  29.  
  30.     FILE *file;
  31.     file = fopen("plik.txt", "wt");
  32.     if (file)
  33.     {
  34.         for (int i = 0; i < 3;++i)
  35.         {
  36.             fprintf(file, "%s %s %d\n", sam[i].marka,sam[i].kolor,sam[i].rocznik );
  37.         }
  38.     }
  39.     fclose(file);
  40.     for (int i = 0; i < 3; ++i)
  41.     {
  42.         printf("\nMarka: %s\n", sam[i].marka);
  43.         printf("Kolor: %s\n", sam[i].kolor);
  44.         printf("Rocznik: %d\n", sam[i].rocznik);
  45.     }
  46.     free(sam);
  47.     _getch();
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment