1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <iostream>
#include <fstream>
#include <algorithm> // for fill_n
#include <cstdlib> // for srand(), rand()
using namespace std;
void Binary_Search (const string& filename, string SearchVal)
{
//fstream file; // what std:: here an not elsewhere?
// ifstream as we're just reading it
// ios::bin as we're treating it as binary, yes?
ifstream file (filename.c_str(), ios::binary);
// don't need these variables yet
//int first, last, middle, BuffSize = 30, lines = 1;
//char buffer[BuffSize];
//bool found = false;
//file.open( filename.c_str());
if (file.is_open())
{
cout << "The file is opened"<< endl;
cout << "\n";
}
else
{
cout << "Error opening file"<< endl;
cout << "\n";
return; // no point continuing Binary_Search() if file failed to open!
}
//char c;
//file.get(c);
//while (file)
//{
// if(c == '\n')
// {
// lines++;
// }
// file.get(c);
//}
//cout << "\n"<<lines;//The amount of lines inside the binary file.
// The binary file doesn't have any lines in it! (see hex dump above)
const unsigned int RECORD_SIZE = 30; // was BUFFER_SIZE
char buffer[RECORD_SIZE] = {0}; // zero init buffer
int recordCount = 0;
int recordWanted = -1;
while (file.read(buffer, RECORD_SIZE))
{
if(SearchVal == buffer)
{
recordWanted = recordCount;
// if this was just a naive search loop could bail out now...
}
// as it's a char buffer and we know the string is null
// terminated as we packed the buffer with zeroes when we
// wrote the file (not a totally safe assumption, but...)
cout << recordCount << " : " << buffer << "\n";
// refill buffer with zeroes for next time round
fill_n (buffer, RECORD_SIZE, 0);
++recordCount;
}
cout << "\n";
cout << "file contains " << recordCount << " records\n";
cout << "\n";
if (recordWanted == -1)
cout << "record wanted could not be found\n";
else
cout << "record wanted is at index " << recordWanted << " records\n";
cout << "\n";
cout << "5 random records:\n";
cout << "\n";
for(int i = 0; i < 5; ++i)
{
int recordIndex = rand() % recordCount;
file.seekg(recordIndex * RECORD_SIZE, ios::beg);
fill_n (buffer, RECORD_SIZE, 0);
file.read(buffer, RECORD_SIZE);
cout << recordIndex << " : " << buffer << "\n";
}
cout << "\n";
/*
So the first one should be 0, last one should be equal to lines.
This is how im planing to get the attributes to do the binary search.
The problem is, lets say, it has to compare line 40 with the input value
to figure out if the input value is greater or smaller then the string @ line 40.
Ive no idea how to get to that line 40... to get the string.
*/
} // functions don't need ; at end
void Create_Bin_File ()
{
// use ifstream and ofstream rather than fstream
ifstream fin ("rezervetie.txt"); // ios::in presumed
ofstream fout ("Binary.bin", ios::binary); // ios::out presumed, and is binary
const unsigned int RECORD_SIZE = 30; // was BUFFER_SIZE
char buffer[RECORD_SIZE] = {0}; // zero init buffer
// use getline() not read() for the input text file
// don't test fin here and then call getline in loop
while (fin.getline (buffer, RECORD_SIZE))
{
fout.write (buffer, RECORD_SIZE);
// refill buffer with zeroes for next time round
fill_n (buffer, RECORD_SIZE, 0);
} // ; not needed here
//fin.close (); no need for close now, as handled by destructor
//fout.close ();
}
int main ()
{
srand(123U); // as it's a test use fixed seed for now (as repeatable)
Create_Bin_File (); // factor out...
string ievade;
cout << "Ievadiet vardu, kuru salidzinat: ";
cin >> ievade;
Binary_Search ("Binary.bin", ievade);
return 0; // as I'm neurotic
}
|