Skip to content

Instantly share code, notes, and snippets.

@Battleroid
Created March 18, 2013 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Battleroid/5186884 to your computer and use it in GitHub Desktop.
Save Battleroid/5186884 to your computer and use it in GitHub Desktop.
Creates binary file filled with random integers, appends information if file already exists.
#include <fstream>
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main () {
// create random array
int array[100];
srand(time(0));
for (int i = 0; i < 100; i++) {
int tmp = rand() % 10 + 1;
// cout << i << " = " << tmp << endl;
array[i] = tmp; // 1-10
}
// open and append
fstream output("Exercise13_13.dat", ios::app | ios::out | ios::binary);
if (output.is_open()) {
output.write((char *) &array, sizeof(array));
output.close();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment