Solution 1: Using text data file to store hash maps:
#include <fstream>
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream file_in;
std::string filename = "inp.txt";
char line[1024] = "\0"; std::streamsize size = 1024;
file_in.open(filename.c_str(), std::ifstream::in);
while (file_in.good() && file_in.getline(line, size, '\n'))
{
char key[256] = "\0", value[256] = "\0";
sscanf_s(line, "KEY: %s VALUE: %s", key, 256, value, 256);
std::cout << key << " " << value << endl;
}
file_in.close();
std::cin.get();
return 0;
}
Solution 2: Using binary files and mmap function:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#define NUMITEMS 10
typedef struct tagHash
{
char key[256];
char value[256];
}HASH, *PHASH;
int main(int argc, char** argv)
{
const char fname[256] = "inp.bin";
int fd = -1;
if ((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) == -1)
{
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
PHASH hash_array = new HASH[NUMITEMS];
memset((void*)hash_array, 0x00, sizeof(HASH) * NUMITEMS);
for (int i = 0; i < NUMITEMS; i++)
{
sprintf(hash_array[i].key,"KEY: %d",i);
sprintf(hash_array[i].value,"VALUE: %d",i);
}
write(fd, (void*)hash_array, sizeof(HASH) * NUMITEMS);
long cur_pos = 0L, file_size = 0L;
cur_pos = lseek(fd, 0, SEEK_CUR);
file_size = lseek(fd, 0, SEEK_END);
lseek(fd, cur_pos, SEEK_CUR);
HASH* hash_ptr = NULL;
if ((hash_ptr = reinterpret_cast<HASH*>(mmap(0, file_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))) == MAP_FAILED)
{
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
while (hash_ptr != NULL)
{
printf("key = %s value = %s\n",hash_ptr->key, hash_ptr->value);
hash_ptr++;
}
close(fd);
return 0;
}
Solution 3: Filling QtHash with data item from a file
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <QHash>
#include <QString>
#define NUMITEMS 10
typedef struct tagHash
{
char key[256];
char value[256];
}HASH, *PHASH;
int main(int argc, char** argv)
{
const char fname[256] = "inp.bin";
int fd = -1;
if ((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) == -1)
{
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
PHASH hash_array = new HASH[NUMITEMS];
memset((void*)hash_array, 0x00, sizeof(HASH) * NUMITEMS);
for (int i = 0; i < NUMITEMS; i++)
{
sprintf(hash_array[i].key,"KEY: %d",i);
sprintf(hash_array[i].value,"VALUE: %d",i);
}
write(fd, (void*)hash_array, sizeof(HASH) * NUMITEMS);
long cur_pos = 0L, file_size = 0L;
cur_pos = lseek(fd, 0, SEEK_CUR);
file_size = lseek(fd, 0, SEEK_END);
lseek(fd, cur_pos, SEEK_CUR);
HASH* hash_ptr = NULL;
if ((hash_ptr = reinterpret_cast<HASH*>(mmap(0, file_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0))) == MAP_FAILED)
{
close(fd);
perror("Error mmapping the file");
exit(EXIT_FAILURE);
}
QHash<QString, QString> hashmap;
while (hash_ptr != NULL)
{
hashmap.insert(QString(hash_ptr->key), QString(hash_ptr->value));
hash_ptr++;
}
close(fd);
return 0;
}
P.S. If you still want to experiment with mmap, here's the
LINK[^] to the online resources explaining how to use the mmap function.
That's it.