peterzig

[PO] Struktury z pliku

Feb 29th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. // struktury.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "conio.h"
  6. #include <iostream>
  7.  
  8. struct Samochod
  9. {
  10.     char marka[20];
  11.     char kolor[20];
  12.     int rocznik;
  13. };
  14.  
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19.     Samochod *sam = new Samochod[3], *buf = new Samochod[3];
  20.     for (int i = 0; i < 3; i++)
  21.     {
  22.         cout << endl << "Marka: ";
  23.         cin.get(sam[i].marka, 20);
  24.         cout << "Kolor: ";
  25.         cin >> sam[i].kolor;
  26.         cout << "Rocznik: ";
  27.         cin >> sam[i].rocznik;
  28.         cin.get();
  29.     }
  30.  
  31.     FILE *file;
  32.     file = fopen("plik.txt", "wb");
  33.     if (file)
  34.     {
  35.         fwrite(sam, sizeof(Samochod), 3, file);
  36.     }
  37.     fclose(file);
  38.  
  39.     file = fopen("plik.txt", "rb");
  40.     if (file)
  41.     {
  42.         fread(buf, sizeof(Samochod), 3, file);
  43.     }
  44.  
  45.     for (int i = 0; i < 3; i++)
  46.     {
  47.         cout << endl << "Marka: " << buf[i].marka << endl;
  48.         cout << "Kolor: " << buf[i].kolor << endl;
  49.         cout << "Kolor: " << buf[i].rocznik;
  50.     }
  51.  
  52.     delete(sam);
  53.     delete(buf);
  54.     _getch();
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment