Offsets of the words in the file.
-
Hello All, I am trying to read a text file & add the words and offset in to map table. Can anyone please tell me how to calculate the offset of the words in the file ??
#include #include #include #include using namespace std; typedef map FileMap; #define SIZE 255 char sBuffer[SIZE]; int main() { string word; int offset; FileMap FMap; ifstream fin; fin.open("D:\\Demo.txt",ios::in); while(!fin.eof()) { fin >> sBuffer; // FMap[sBuffer]=offset; } fin.close(); cout << "\n Enter Word:"; cin >> word; offset=FMap[word]; cout << "\n Word Offset is :" << offset; return 0; }
Thanking you, Suresh HC. -
Hello All, I am trying to read a text file & add the words and offset in to map table. Can anyone please tell me how to calculate the offset of the words in the file ??
#include #include #include #include using namespace std; typedef map FileMap; #define SIZE 255 char sBuffer[SIZE]; int main() { string word; int offset; FileMap FMap; ifstream fin; fin.open("D:\\Demo.txt",ios::in); while(!fin.eof()) { fin >> sBuffer; // FMap[sBuffer]=offset; } fin.close(); cout << "\n Enter Word:"; cin >> word; offset=FMap[word]; cout << "\n Word Offset is :" << offset; return 0; }
Thanking you, Suresh HC.What do you mean by offset exactly ? The index (in the file) of the first character of the word ? If yes, in your loop you can simply increment the offset counter by the size of the last word read:
while(!fin.eof()) { fin >> sBuffer; offset += strlen(sBuffer) + 1; // +1 for the whitespace character FMap[sBuffer]=offset; }
There is also a flaw in your design: if the user enters a word that was not stored in the map, then you'll get a crash. It is better to check if the key exist in the map (using the find function).
Cédric Moonen Software developer
Charting control [v1.1] -
What do you mean by offset exactly ? The index (in the file) of the first character of the word ? If yes, in your loop you can simply increment the offset counter by the size of the last word read:
while(!fin.eof()) { fin >> sBuffer; offset += strlen(sBuffer) + 1; // +1 for the whitespace character FMap[sBuffer]=offset; }
There is also a flaw in your design: if the user enters a word that was not stored in the map, then you'll get a crash. It is better to check if the key exist in the map (using the find function).
Cédric Moonen Software developer
Charting control [v1.1]