Advertisement
zahra315

Kopija

May 23rd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. include "stdafx.h"
  2. #include <iostream>
  3. #include<fstream>
  4. using namespace std;
  5.  
  6. template<typename Tip>
  7. void f_snimi(ofstream& f, Tip &tip)
  8. {
  9.     int v = sizeof(Tip);
  10.     f.write((char*)&tip, v); //kad je write to je odmah binarni fajl
  11. }
  12.  
  13. int main()
  14. {  
  15.     char x='s';
  16.     const char * naziv = "test";
  17.     ofstream unos(naziv);
  18.     f_snimi(unos, x);
  19.     if (!unos.fail()) // provjerava je li ima ista u fajlu
  20.         cout << "Uspjesno kreiran file" << endl;
  21.     unos.close();
  22.    
  23.     system("PAUSE>0");
  24.     return 0;
  25. }
  26.  
  27. ---------------******************************************************-------------------------
  28. // ConsoleApplication12.cpp : Defines the entry point for the console application.
  29. //
  30.  
  31. #include "stdafx.h"
  32. #include <iostream>
  33. #include<fstream>
  34. using namespace std;
  35. struct Datum
  36. {
  37.     int _dd, _mm, _gggg;
  38.     void input(int d, int m, int g)
  39.     {
  40.         _dd = d;
  41.         _mm = m;
  42.         _gggg = g;
  43.     }
  44. };
  45. template<typename Tip>
  46. void f_snimi(ofstream& f, Tip &tip)
  47. {
  48.     int v = sizeof(Tip);
  49.     f.write((char*)&tip, v); //kad je write to je odmah binarni fajl
  50.     f.close();
  51. }
  52. template<typename Tip>
  53. void file_ispis(ifstream &f, Tip &x)
  54. {
  55.     int v = sizeof(Tip);
  56.     f.read((char*)&x, v);
  57.     f.close();
  58.  
  59. }
  60.  
  61.  
  62. int main()
  63. {  
  64.     Datum dd1, dd2;
  65.     int a = 10, b ;
  66.     dd1.input(27,07, 1998);
  67.     ofstream unos("dat", ios::binary);
  68.     f_snimi(unos, dd1);
  69.     if (!unos.fail())
  70.         cout << "***FILE USPJESNO KREIRAN***" << endl;
  71.  
  72.     ifstream ispis("dat");
  73.     file_ispis(ispis, dd2);
  74.  
  75.     ofstream upis("dat2");
  76.     f_snimi(upis, a);
  77.  
  78.     ifstream isspis("dat2");
  79.     file_ispis(isspis, b); //jer tako kopiramo ono sto pise u fajlu
  80.     cout << b;
  81.  
  82.     system("PAUSE>0");
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement