Advertisement
Tvor0zhok

Файлы III

Nov 18th, 2020 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include "iostream"
  2. #include "fstream"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. setlocale(LC_ALL, "Russian");
  8. ofstream out("fibonacci.dat", ios::binary);
  9. int n;
  10. cout << "n = ";
  11. cin >> n;
  12. int a1 = 1, a2 = 1, a;
  13.  
  14. //записываем данные в двоичный файл
  15. for (int i = 1; i <= n; ++i)
  16. {
  17. if (i <= 2) { a = 1; out.write((char*)&a, sizeof(a)); }
  18. else
  19. {
  20. a = a1 + a2;
  21. a1 = a2;
  22. a2 = a;
  23. out.write((char*)&a, sizeof(a));
  24. }
  25. }
  26.  
  27. out.close();
  28. ifstream in("fibonacci.dat", ios::binary);
  29. cout << "Номер:" << '\t' << "Элемент:" << endl;
  30. int k = 0;
  31. in.seekg(0);
  32. while (in.peek() != EOF)
  33. {
  34. ++k; in.read((char*)&a, sizeof(int));
  35. if (k % 3 != 0) cout << k << '\t' << a << endl;
  36. }
  37.  
  38. in.close();
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement