Reputation: 832
So Basically I am trying to "populate" a file with 10^3 completely random numbers, so I could add them later to a Binary search tree. Here is the populate function I worked on so far:
void populateFile(BinarySearchTree b) {
int random_integer;
srand( time( NULL ) );
std::ofstream myfile;
string line;
myfile.open ("output.txt");
myfile << "Writing this to a file: ";
for(int index=0; index<1000; index++)
{
random_integer = (rand()%1000)+1;
cout << random_integer << endl;
myfile << random_integer;
}
myfile.close();
int value;
ifstream file ("output.txt");
if (file.is_open())
{
while ( getline (file,line) )
{
value = std::stoi(line);
b.insert(value);
}
myfile.close();
}
else cout << "Unable to open file";
}
But I cannot seem to write to a file, I can just see the numbers on the console then the program crashes.
My second concern is as follows: I want to add those same numbers to a binary search tree. I already have a class and an dd function, but I have no idea how to continue. Then I want to be able to delete them from the BST completely at random.
I already have a remove function written. How is this possible? Any ideas will be greatly appreciated. P.S: I am fairly new to C++, I am sorry if my questions sounds silly for you.
Upvotes: 4
Views: 6591
Reputation: 206707
I think the solution to your problem lies in the comment by @Praetorian:
You probably want
myfile << random_integer << '\n';
. Otherwisestoi
will throwout_of_range
, which is probably the cause of the crash.
I have a few generic suggestions regarding your function.
Separate your function into two
-- one for writing to the file
-- one for reading from the file and populating a BST.
Don't use hard coded names of files or global variables in the functions. Make them arguments to the function.
Always check for the status of IO operations. Deal with failures.
Seed the random number generator in main
or a driver function. If you call the function that generates random numbers multiple times, you won't need to seed the random generator again.
void populateFile(int count,
std::string const& file)
{
std::ofstream myfile(file);
if (!myfile )
{
// Deal with error.
return;
}
for(int index=0; index<count; index++)
{
random_integer = (rand()%1000)+1;
myfile << random_integer << "\n";
}
}
void readFileAndBuildBST(std::string const& file,
BinarySearchTree& b)
{
std::ifstream myfile(file);
if (!myfile )
{
// Deal with error.
return;
}
int number;
while ( myfile >> number )
{
b.insert(number);
}
}
void driver()
{
// Seed the random number generator.
srand( time( NULL ) );
// Populate the file
std::string file("output.txt");
populateFile(1000, file);
// Read the data from the file and flesh out the BST.
BinarySearchTree b;
readFileAndBuildBST(file, b);
}
With the functions divided into two, you can test one function at a time. If there is a problem in one function, you can debug the problem and fix it before working on the other function.
Upvotes: 5
Reputation: 1304
Change
cout << random_integer << endl;
myfile << random_integer;
to:
myfile << random_integer << endl;
On a sidenote, if you need data only during the lifetime of the program, you might want to use a buffer or even add the numbers directly into your binary search tree.
Upvotes: 0