Thread: File I/O - Hash files

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    25

    File I/O - Hash files

    I'm having problems with the basics of binary file i/o. I really don't know too much about file i/o so far, so I'll try to describe my question as best I can.

    What I'm trying to do is to create a simple hash file with 7 buckets while each bucket holds 2 entries and utilize them. So far, I been able to create a blank table, but I haven't been able to write to any of the buckets, at least in the way that I want to. I find the correct offset but the blank table gets wiped up until the point where I want to write and after it gets removed. I checked this with a hex editor.

    So, what I think i want to ask is - how can I open a existing file to modify in binary? or how do i change one of the entries in the blank hash table/file?

    I think what I've put in bold below is what has been causing my problems. I think im lost : /

    thanks for any insight/advice.

    (most of this is incomplete)

    funcs.cpp
    Code:
    #include "func.h"
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    HashFile::HashFile(int n, int r)
    {
    
    	tableSize = n;
    	bucketSize = r;
    	
    	/******************* initialize table assist *********/
    	tableAssist = new int[tableSize];
    	for (int j = 0; j < tableSize; j++)
    	{
    		tableAssist[j] = 0;
    	}
    
    	/**** create Table size * bucket size blank entries ****/
    	DataRec tempDataRec;
    	tempDataRec.id = 0;
    	ofstream outfile("Bout", ios::out);
    	for (int i = 0; i < tableSize * bucketSize; i++)
    	{
    		outfile.seekp( i * sizeof(DataRec) );
    		outfile.write( (char *) &tempDataRec, sizeof(DataRec) );
    	}
    	outfile.close();
    }
    
    int HashFile::store(const DataRec &data_rec)
    {
    
    	
    	ofstream outfile;
    	outfile.open("Bout", ios::out);
    
    	int temp = h(data_rec.id);
    
    	if (tableAssist[temp] == 0)
    	{
    		outfile.seekp( temp * sizeof(DataRec) );
    		outfile.write( (char *) &data_rec, sizeof(DataRec) );
    		tableAssist[temp]++;
    		return 0;
    	}
    	if (tableAssist[temp] == 1)
    	{
    		outfile.seekp( (temp+1) * sizeof(DataRec) );
    		outfile.write( (char *) &data_rec, sizeof(DataRec) );
    		tableAssist[temp]++;
    		return 0;
    	}
    	if (tableAssist[temp] == 2) //h2 - double hash
    	{
    		temp = h2(data_rec.id);
    	}
    	
    /*
    */
    
    	return -1;
    }
    
    
    int HashFile::h(int k)
    {
    	return (k % tableSize);
    }
    
    int HashFile::h2(int k)
    {
    	return (k % (tableSize - 1) + 1);
    }
    main.cpp
    Code:
    #include <iostream>
    #include "func.h"
    using namespace std;
    
    int main()
    {
    	DataRec temp;
    	temp.id = 1;
    	HashFile hashobj(7, 2);
    	return 0;
    }
    func.h
    Code:
    #ifndef FUNC_H
    #define FUNC_H
    #include <fstream>
    //using namespace std;
    
    struct DataRec
    {
    	int id;
    	char name[20];
    };
    
    class HashFile
    {
    public:
    	HashFile(int, int);
    	int h(int);
    	int h2(int);
    	int store(const DataRec&);
    	int retrieve(DataRec&);
    private:
    	int tableSize;
    	int bucketSize;
    	int * tableAssist;
    };
    #endif

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It would be so much better to do this in memory since that supports the speed of hashing algorithms. File IO is pretty slow, if you haven't been informed.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Quote Originally Posted by whiteflags View Post
    It would be so much better to do this in memory since that supports the speed of hashing algorithms. File IO is pretty slow, if you haven't been informed.
    Ya, I was told about his during lecture. The purpose of this is to simulate secondary storage access : /

    Code:
    ofstream outfile;
    outfile.open("Bout", ios::out);
    Does this mean open "Bout" for output as a new file? or if it exists already is it overwritten?
    or does it open up the file and the get/put pointers are at the end or the beginning of the file?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    What open does depends on the mode you call it with, you can look up the modes in any sufficient reference, like your text, or online through a search engine.

    In the specific instance you coded in the post, ios:: out merely readies a stream for output. "Bout" would be a filename, and any previous "Bout" file is overwritten for the purpose of the new file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  2. Question about file I/O for configuration files.
    By sup_stephen in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 11:57 PM
  3. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM