I want all the records to be of same size. This is because I want to search for the records, for which I am using interpolation search, as the numbe of records is very high.
If I output them individually, the strings will take variable length, and hence all the lengths of records will be different, and I'll have to apply linear search only.
I tried to read the file byte by byte, then I realised, that the problem is while writing the record.
The following is the definition of th class:
Code:
enum prtype{error=1, notice, info};
struct priority
{
char msg[15];
char pr;
enum prtype type;
};
enum msgcat{lineupdown=1,others};
struct message
{
enum msgcat category;
char updown;
char mssg[170];
char interface[15];
};
class record
{
unsigned long int date, time;
priority prrty;
int ip[4];
message m1;
public:
record()
{
date=time=0;
prrty.msg[0]='\0';prrty.pr=0;prrty.type=0;
ip[0]=ip[1]=ip[2]=ip[3]=0;
m1.mssg[0]='\0';m1.category=0;m1.interface[0]='\0';m1.updown=0;
}
int extractinfo(char *);
void displayall();
void writetofile(char *);
int checkip(char *);
void filename(char *);
};
Now, when I am reading it byte by byte, for a particular file created by the program, which has only four records in form of the object of this class, the first four bytes are occupied by date, the next four by time, in the last two records; but there are only three bytes of time in the first two cases. one byte is missing, and its the third byte( out of four byte long), not the first or last.
I am not able to understand the reason.
If you can recommend a better way to save the record, then it also can be very helpful.
And code, can I mail it to you??
Else, I am using
Code:
fout.write((char*)this,sizeof(record));
fout.read((char*)this,sizeof(record));
only to read and write records.
Thanks very much for the help.