string - C++ Function that uses BinarySearch algorithm (.bin file) -
i've make function checks if specific word exists in .bin file. want use binary search algorithm. thing is, i've read .bin file, got confused (as there's no lines, right?). function doesn't work me. says 'specific word' (entered user) doesn't exist, though does. nice.
#include <iostream> #include <string> #include <fstream> #include <cstring> #include <algorithm> using namespace std; const int buffer_size = 30; void create_bin_file () { ifstream fin ("example.txt"); ofstream fout ("binary.bin", ios::binary); const unsigned int record_size = 30; // buffer_size char buffer[record_size] = {0}; // 0 init buffer while (fin.getline (buffer, record_size)) { fout.write (buffer, record_size); // refill buffer zeroes next time round fill_n (buffer, record_size, 0); } fin.close (); fout.close (); } void binary_search (const string& filename, string searchval) { ifstream file (filename.c_str(), ios::binary); if (file.is_open()) { cout << "the file opened"<< endl; cout << "\n"; } else { cout << "error opening file"<< endl; cout << "\n"; return; // no point continuing binary_search() if file failed open! } const unsigned int record_size = 30; // buffer_size char buffer[record_size] = {0}; // 0 init buffer int recordcount = 0; int recordwanted = -1; while (file.read(buffer, record_size)) { if(searchval == buffer) { recordwanted = recordcount; // if naive search loop bail out now... } cout << recordcount << " : " << buffer << "\n"; // refill buffer zeroes 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 not found\n"; else cout << "record wanted @ index " << recordwanted << " records\n"; cout << "\n"; } int main() { create_bin_file(); string word; cout << "enter word, want find in file: " << endl; cin >> word; binary_search("binary.bin", word); return 0; }
task: "write program in c ++. if program works file, should not copy entire content of file in operative memory. file component means fixed-length record. h7. write program puts standard c ++ reserved words in ordered table (ordered table far understood means these words in alphabetic order). write function, which, using binary search, checks if input string (length 30) c ++ reserved word or not. table should made direct access file. c ++ reserved program should read text file."
grek40 solution on binarysearch function:
so make record function:
std::string getrecord(std::ifstream& infile, int pos) { char buffer[record_size]; // clear possible flags eof, before moving read position infile.clear(); // set file read position requested record position infile.seekg(pos * record_size, std::ios::beg); infile.read(buffer, record_size); // note: automatic conversion char[] std::string return buffer; }
and binary search funtion: (solved - working!)
void binary_search (const string& filename, string searchval) { ifstream file (filename.c_str(), ios::binary); if (file.is_open()) { cout << "the file opened"<< endl; cout << "\n"; } else { cout << "error opening file"<< endl; cout << "\n"; return; // no point continuing binary_search() if file failed open! } int pos = 0; int lowerlimit = 0; int recordcount = 73; // calculated before[i'll change part, when function working] // @ point, there's 73 records in .bin file char buffer[record_size] = {0}; // 0 init buffer (while loop overwrite record values) int upperlimit = recordcount; while ( (lowerlimit < upperlimit) ) // searching long doesn't find { pos = (lowerlimit + upperlimit) / 2; std::string buffer = getrecord(file, pos); if (buffer == searchval) { cout << "found!"; lowerlimit = 1; // stopping (if found!) upperlimit = 0; // stopping } else if (searchval > buffer) { lowerlimit = pos + 1; } else if (searchval < buffer) { upperlimit = pos; } } }
as far can see, have solution move given words textfile binary file , able find words in binary file if exist there.
i suppose created binary file sorted records of equal length (30), text part of each record zero-terminated.
now, lets create function takes opened binary file stream , record position , returns string @ record position:
std::string getrecord(std::ifstream& infile, int pos) { char buffer[record_size]; // clear possible flags eof, before moving read position infile.clear(); // set file read position requested record position infile.seekg(pos * record_size, std::ios::beg); infile.read(buffer, record_size); // note: automatic conversion char[] std::string return buffer; }
for binary search, should define upper , lower limit search position. note upper limit lastitemposition + 1
, never access position in 0 based index.
int lowerlimit = 0; int upperlimit = recordcount; // count when reading lines in .txt
you need search result long didn't find , lowerlimit < upperlimit
.
your next search word @ position = (lowerlimit + upperlimit) / 2;
.
compare word search text. on equality, done.
if word less search text, result position can @ higher indices looked at. need adjust lowerlimit = position + 1
if word greater search text, result position can @ lower indices looked at. upperlimit = position
you repeat search adjusted upper , lower limits described.
Comments
Post a Comment