Skip to content

Instantly share code, notes, and snippets.

@jamby77
Created March 13, 2018 22:24
Show Gist options
  • Save jamby77/276efbe67daf34676e5d301be95106ba to your computer and use it in GitHub Desktop.
Save jamby77/276efbe67daf34676e5d301be95106ba to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream> // std::ifstream, std::ofstream
using namespace std;
int main()
{
int N, num, res;
streampos size;
ofstream outfile("new.txt", ios::out | ios::binary);
if (!outfile.is_open())
{
cout << "Can't open file for writing" << '\n';
}
cout << "how many numbers do you want?" << endl;
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> num;
outfile.write((char *)&num, sizeof(int));
}
outfile.close();
ifstream file("new.txt", ios::in | ios::binary | ios::ate);
size = sizeof(num);
if (file.is_open())
{
file.seekg(0, ios::beg);
for (int i = 0; i < N; i++)
{
if ((i + 1) % 3 != 0)
{
continue;
}
file.seekg(i * size, ios::beg);
file.read((char *)&res, size);
cout << res << endl;
}
file.close();
}
else
cout << "Unable to open file";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment