Esempio n. 1
0
void fileio()
{
    string filename;
    int n;
    
    cout << endl;
    cout << "Please enter the name of your file." << endl;
    cout << "User Entry: ";
    cin >> filename;
    
    cout << endl;
    cout << "Processing file..." << endl;
    
    //***start clock***//
    clock_t start, finish;
    double dur;
    start = clock();
    //*****************//
    
    ifstream filein;
    filein.open(filename);
    
    //grab n number of lines
    string nlines;
    getline(filein, nlines, ' ');
    n = atoi(nlines.c_str());
    
    //grab each line, process for words, and map to hashtable
    while (!filein.eof())
    {
        string line;
        getline(filein, line);
        
        stringstream stream(line);
        while (!stream.eof())
        {
            string word;
            getline(stream, word, ' ');
            
            //accounts for more than one white space by looking for first letter
            size_t letter = word.find_first_of("abcdefghijklmnopqrstuvwxyz");
            
            //create a sub-string to remove the extra spaces
            string substring =  word.substr(letter, word.npos);
            
            //hash the substring, then add its corresponding line to the table
            HashFunction H;
            int HashKey = H.Hash(substring);
            Table.add(HashKey, line);
        }
    }
    
    cout << "Processing Complete." << endl;
    
    //***stop clock***//
    finish = clock();
    dur = (double)(finish - start);
    dur /= CLOCKS_PER_SEC;
    cout << "Elapsed seconds: " << scientific << dur << endl;
    //****************//
}