Guest User

Untitled

a guest
Mar 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream> // std::ifstream, std::ofstream
  3. using namespace std;
  4. int main()
  5. {
  6. int N, num, res;
  7. streampos size;
  8. ofstream outfile("new.txt", ios::out | ios::binary);
  9. if (!outfile.is_open())
  10. {
  11. cout << "Can't open file for writing" << '\n';
  12. }
  13.  
  14. cout << "how many numbers do you want?" << endl;
  15. cin >> N;
  16.  
  17. for (int i = 0; i < N; i++)
  18. {
  19. cin >> num;
  20. outfile.write((char *)&num, sizeof(int));
  21. }
  22.  
  23. outfile.close();
  24.  
  25. ifstream file("new.txt", ios::in | ios::binary | ios::ate);
  26. size = sizeof(num);
  27.  
  28. if (file.is_open())
  29. {
  30. file.seekg(0, ios::beg);
  31. for (int i = 0; i < N; i++)
  32. {
  33. if ((i + 1) % 3 != 0)
  34. {
  35. continue;
  36. }
  37. file.seekg(i * size, ios::beg);
  38. file.read((char *)&res, size);
  39. cout << res << endl;
  40. }
  41. file.close();
  42. }
  43. else
  44. cout << "Unable to open file";
  45. return 0;
  46. }
Add Comment
Please, Sign In to add comment