Append 100 random integers to binary file with C++/Cpp programming
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
ofstream out("Exercise13_13.dat", (ios::out | ios::binary | ios_base::app));
if(!out){
cerr << "Error: failed to open file!" << endl;
return 1;
}
srand(time(0));
int num;
int bytes = sizeof(int);
const char *addr = reinterpret_cast<const char*>(&num);
for(int i = 1; 100 >= i; i++){
num = rand() % 100;
out.write(addr, bytes);
}
out.close();
return 0;
}
Download
Comments
Post a Comment