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();
}

i suspect the problem might be file i/o related; however, i do not see any file i/o at all. i suspect these operations occur in the getblock() function (and it's variants) but I am not sure.

one thing i would recommend though, is if you are attempting to use a fstream object more than once.. for a different file, the make sure to clear the object before re-using it.

In getblock() ?
Well, here is getblock() from the filesystem class in case anything sticks out.

int Filesys::getblock(string file, int blocknumber, string& buffer)
{
	if(!found(file))
    {
        return 0; // file does not exist  
    }
    disk.getblock(blocknumber, buffer);
	return 1;
}

which uses getblock from the disk class...

int Pdisk::getblock(int blocknumber, string& buffer)
{
    fstream input;
    input.open (diskname.c_str(), ios::in);
    if (input.bad())
    {
        return 0;
    }
    if (input.is_open())
    {
        input.seekg(blocknumber * blocksize + 1, ios::beg);
        char x;
        for(int i = 0; i < blocksize; i++)
        {
            x = input.get();
            buffer = buffer + x;
        }
        input.close();
        return 1;
    }
}
This article has been dead for over six months. Start a new discussion instead.