//
//
//
//
// Lab. Calcolo II - Esempio di codice
//
//
//
//
//
//
//
// Example code: Very simple fstream usage. (20060728 prelz@mi.infn.it)
#include
#include
#include
#include
int main(int argc, char *argv[])
{
std::fstream rnd_in;
rnd_in.open("/dev/urandom", std::ios::in);
if (!rnd_in) // Any error ? Should *always* check !
{
std::cerr << "Error opening /dev/urandom for read: "
<< std::system_error(errno, std::system_category()).what()
<< "." << std::endl;
return 1;
}
for(int i=0; i<=100 && rnd_in ; ++i)
{
// Read one basic I/O unit: a char
char c;
rnd_in.get(c);
// type cast to remove the sign - I/O operates
// on signed chars.
unsigned int pick = static_cast(c) % 90;
std::cout << "Random pick # " << i << " == " << pick << std::endl;
}
if (!rnd_in)
{
std::cerr << "Error reading from /dev/urandom: "
<< std::system_error(errno, std::system_category()).what()
<< "." << std::endl;
rnd_in.close();
return 1;
}
rnd_in.close();
return 0;
}