I have read a file with around 120k words so i try to do it fast. have seen the:
int x = setvbuf(fp, (char *)NULL, _IOFBF, BSZ);
assert( x == 0 && fp != NULL );
option but it takes more than a second ( 1 mb file) so now i tried this method :
f
open_s (&pFile,DICT,"rb");
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
how do i continue from here? buffer holds a list of words and i want to get them one by one as fast as possible because im building a multimap with those words.
need to be something like this:
while(token!=NULL){
DB.insert(pair<unsigned int,string>((unsigned)strlen(token),token));
//cout<< buffer;
strtok_s(buffer,delims,&context);
}
just getting the words and inserting to a database
thank you!
EDIT:
THIS WORKS :
char* context = NULL;
char delims[] = " ,\t\n";
char* token = NULL;
FILE * pFile;
long lSize;
char * buffer;
size_t result;
fopen_s (&pFile,DICT,"rb");
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
token = strtok (buffer," \n");
while (token != NULL)
{
DB.insert(pair<unsigned int,string>((unsigned)strlen(token),token));
token = strtok (NULL, " \n");
}
but takes too long... i need something in 0.5> seconds approximetly... file size is only 1mb