Hello all,
I am building a small file system in c++ that builds a table in a file called test.txt from information on another file. I am trying to copy the information over and then access a specific part of it based on the date. The data is copied over to the filesystem but I cannot access it again. Can anyone tell me what I am doing wrong? I am putting in the files that do this perticular function below please let me know if you see any obvious mistakes. Every time I put in a date right now, it never returns anything.
TABLE.H
#ifndef table_h
#define table_h
#include "filesys.h"
#include <string>
class Table
{
public:
Table(Filesys& filesy, string flatfile, string indexfile);
int Build_Table(string input_file);
int Search(string value);
private:
string flatfile;
string indexfile;
Filesys *filesystem;
int IndexSearch(string value);
};
#endif
TABLE.CPP
#include "table.h"
#include <algorithm>
#include <string>
Table::Table(Filesys &file_sys, string flat_file, string index_file){
filesystem = &file_sys;
flatfile = flat_file;
indexfile = index_file;
filesystem -> newfile(flatfile);
filesystem -> newfile(indexfile);
}
int Table::Build_Table(string input_file){
string block, record, date;
int x;
vector<string> block;
stringstream ss;
ifstream file(input_file.c_str());
if(!file.good())
{
cout << "Input file not found, could not open input file...\n";
return -1;
}
getline(file, record);
while(!file.eof())
{
x = record.find("*");
date = record.substr(0,x);
//remove white spaces
x = date.find(" ");
if(x != string::npos)
{
date = date.substr(0,x);
}
ss << date << " " ;
int flatfileblocknumber = filesystem -> addblock(flatfile,record);
ss << flatfileblocknumber << " ";
filesystem -> addblock(indexfile, ss.str());
block.clear();
ss.str(" ");
ss.clear();
getline(file, record);
}
}
int Table::Search(string value)
{
string buffer, date, end, type, place, ref, desc;
vector<string> record;
int blocknumber = IndexSearch(value), lastposition, pos;//////////////////
if(blocknumber == -1){
cout << "Couldn't find the record..." << endl;
return -1;
}
filesystem -> getblock(flatfile,block_number,buffer);
//slik way of tokanizing the buffer
lastposition = buffer.find_first_not_of("*",0);
pos = buffer.find_first_of("*", lastposition);
while (pos != string::npos || lastposition != string::npos){
record.push_back( buffer.substr(lastposition, pos-lastposition));
lastposition = buffer.find_first_not_of("*", pos);
pos = buffer.find_first_of("*", lastposition);
}
cout << "Record found: " << endl;
cout << "\tDate: " << record.at(0) << endl;
cout << "\tEnd: " << record.at(1) << endl;
cout << "\tType: " << record.at(2) << endl;
cout << "\tPlace: " << record.at(3) << endl;
cout << "\tReference: " << record.at(4) << endl;
cout << "\tDescription: " << record.at(5) << endl << endl;;
return 0;
}
int Table::IndexSearch(string value)
{
int current_block = filesystem -> getfirstblock(indexfile), blocknumber;
string block, date;
stringstream ss;
while(current_block != 0)
{
filesystem -> getblock(indexfile, current_block, block);
ss.str(block);
ss >> date >> blocknumber;
if(date == value)
{
return blocknumber;
}
current_block = filesystem -> nextblock(indexfile, current_block);
}
return -1;
}
DRIVER.CPP
#include <iostream>
#include <fstream>
#include "table.h"
#include "fileSys.h"
#include "disk.cpp"
#include "filesys.cpp"
#include "table.cpp"
using namespace std;
//Drivers
int main()
{
Pdisk disk("test.txt",128,128);
Filesys fs(disk);
Table tb(fs, "FF", "IF");
tb.Build_Table("data.txt");
string x ;
cout << "Enter the Date: ";
cin >> x;
cout << endl;
tb.Search(x);
fs.fsclose();
}