Hi,
I'm trying to write an array of linked lists to disk using c++.I'm running this program on Linux.My data structures are as follows...

A Hashtable class which holds the array of linked lists...
A linked list class which also has a writetodisk function..
An indexer class which adds entries to one of 26(one for each alphabet) entries in the array of hashtable object (the hashtable object subsequently adds this entry to a given linked list.

All my classes are working fine in adding searching etc etc except for when i get down to writing to disk..In this case I am writing a few of the linked lists of the hashtable object to disk at a time then another few to another file and so on..
eg) My hashtable has 211 linked lists i write 5 at a time to one file and so on till i have written all 211 linked lists..

I do this for all my 26 hashtable objects.. My problem is that after writing a upto around 22 of these hashtable objects my 23rd is giving problems... it writes a few of its linked lists to disk and then gives me an error ie the ofstream object is null..I have posted the code below for my write function in my hashtable class .. If anyone could give me an idea where i'm going wrong i would appreciate it...

void hashtable::createbuck()
{
int i=0;
while(i<=HSIZE/NOLL)  //HSIZE=211 the no of linked lists
{                     //NOLL the number of linked lists written
string no;      //at a time...
string path="ht/";         //to create the path...
path=path+letter+"/";   //letter is a member fn storing which                                      //letter the hashtable refers to
itoa(i,no);            //convert i to a string
path+=no;
path+=".gig";
//cout<<"PATH: "<<path<<endl; //the path is coming out fine
//check if dir ht exists if not create it and proceed...
DIR *p=opendir("ht/");
if(!p)
{
//need to create ht and subdirs..
mkdir("ht",S_IRUSR|S_IWUSR);
char ch;
for(int i=0;i<26;i++)
{
string d="ht/";
ch='a'+i;
d=d+ch;
mkdir(d.c_str(),S_IRUSR|S_IWUSR);
}
}
ofstream of(path.c_str());
if(!of)
{
cout<<"Hash:Error creating file.."<<endl;
return;
}
for(int j=0;j<NOLL;j++)
if(i*NOLL+j<HSIZE) //call thw writetofile fn for each
ht[i*NOLL+j].writetofile(of);  //linkedlist
of.close();
i++;
}


}

A function in the indexer class calls this function for each of the 26 hashtable objects...
Thanks a ton,
Piyush

Recommended Answers

All 2 Replies

Please put the code in [ code ] [ / code] tags... it'll be easier to read.

Where is the error about null ofstream happening?

Here?
ht[i*NOLL+j].writetofile(of); //linkedlist

<code>
void hashtable::createbuck()
{
int i=0;
while(i<=HSIZE/NOLL) //HSIZE=211 the no of linked lists
{ //NOLL the number of linked lists written
string no; //at a time...
string path="ht/"; //to create the path...
path=path+letter+"/"; //letter is a member fn storing which //letter the hashtable refers to
itoa(i,no); //convert i to a string
path+=no;
path+=".gig";
//cout<<"PATH: "<<path<<endl; //the path is coming out fine
//check if dir ht exists if not create it and proceed...
DIR *p=opendir("ht/");
if(!p)
{
//need to create ht and subdirs..
mkdir("ht",S_IRUSR|S_IWUSR);
char ch;
for(int i=0;i<26;i++)
{
string d="ht/";
ch='a'+i;
d=d+ch;
mkdir(d.c_str(),S_IRUSR|S_IWUSR);
}
}
ofstream of(path.c_str());
if(!of)
{
cout<<"Hash:Error creating file.."<<endl;
return;
}
for(int j=0;j<NOLL;j++)
if(i*NOLL+j<HSIZE) //call thw writetofile fn for each
ht[i*NOLL+j].writetofile(of); //linkedlist
of.close();
i++;
}

}
</code>
My error is that after writing a few linked lists to disk the ofstream object of keeps returnin null...and thus "Hash:Error creating file" keeps occuring...

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.