Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Offsets of the words in the file.

Offsets of the words in the file.

Scheduled Pinned Locked Moved C / C++ / MFC
iostutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Suresh H
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • S Suresh H

      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.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        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]

        S Offline
        S Offline
        Suresh H
        wrote on last edited by
        #3

        Thanks Cedric Moonen i will check it.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups