/*
Random Line 1 - The program copies random line from one file in other.
It is good for servers, where You need a random message to pop-up.
Programming by sikamikaniko
*/
#include<iostream.h>
#include<fstream.h>
#include<time.h>
#include<stdlib.h>
int countln(int ln); // functions prototype
ofstream ptr_write; //pointer to the file, the text will be written
ifstream ptr_read; //pointer to the file, the program will read from
// main function ---
main()
{
int i_ctrln=0;
int ln=0;
int i_gotoln=ln;
char c_ctrln;
char c_joke;
ln=countln(ln); //
srand(time(NULL)); //randomizacion
i_gotoln=rand()%ln; //
ptr_read.open("jokes.txt", ios::in);
while(ptr_read.get(c_ctrln)) // finds the line, that must be copied
{
if(c_ctrln=='\n')
{
i_ctrln++;
}
if(i_ctrln==i_gotoln)
{break;}
}
ptr_write.open("joke.txt", ios:ut);
while(ptr_read.get(c_joke)) // copies the line in jokes.txt
{
cout << c_joke;
ptr_write << c_joke;
if(c_joke=='\n')
{
break;
}
}
ptr_read.close();
ptr_write.close();
return 0;
}
// Counter ---
int countln(int ln) // counts the lines in jokes.txt
{
char c_ctrln;
ptr_read.open("jokes.txt", ios::in);
while(ptr_read.get(c_ctrln))
{
if(c_ctrln=='\n')
{
ln++;
}
}
ptr_read.close();
ln++;
return (ln);
}