Hi Guys,

Please help me in doing this.

I am trying to read a file using fstream, but my prof wants me to do the same using hash table, which i have know idea. Please tell me hw can i do the same.

The file contains huge data like the size of the block and its respective data.

Please find the code below.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <algorithm>


using namespace std;
using std::ifstream;
using std::ostream;
using std::cout;

int MAX_LENGTH = 0;
int N = 1;

class Entropy
{
        public:

              void readData();
              void process();
//            void write();
              ifstream fin;
              ofstream fout;

};

int main()
{
        Entropy zero_order;
        zero_order.readData();
}


void Entropy::readData()
{
        int n;
        char next;
        cout << "We are about to read the data from the trace.txt";

        cout<<"\n\nHow many bits do you want to read?";
        cin>>n;

        fin.open("trace.txt");
        fout.open("dude.txt");
	
	if(fin.fail())
        {
                cout<<"Opening the input file failed\n";
                exit(1);
        }

        fin.get(next);
        fout<<next;
        n--;

        while(n != 0)
        {
                fin.get(next);
                fout<<next;
                n--;
        }
fin.close();
fout.close();

}

Cheers

I don't see a hash table to read from... ;)

Create a hash function according to your needs and have a look at some standard hashing functions.Once you have a hashing function pass the key parameter of the data you want to write to hashing function and get the hash value corresponding to it.And you can open the file and move the filepointer to the location of your hash value and write the data there. Thats it. Later when you want to look for the data you just need to generate the hash value and corresponding position of data.

This ofcourse is only one among the several ways you can use a hashing.

commented: I have the same coursework to do.can you do an example for us please sir. I would be very grateful +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.