Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ simple reading from a text file (dictionary)

Options
  • 21-07-2006 4:06pm
    #1
    Banned (with Prison Access) Posts: 5,154 ✭✭✭


    Folks,
    I'm looking to go through a text file (just a dictionary - one word per line) and, for each word in the file, perform an action on that word.

    What would be the best way to go about this?

    Cheers,
    Steve.


Comments

  • Closed Accounts Posts: 49 boyracer87


    Heres a simple program that searches through a list imported from a txt file...you could probably use the same principles to make your dictionary

    
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std ;
    
    int BinarySearch(string, vector<string>) ;
    
    main()
    {	int i, Pos ;
    	vector<string> ContactList ;
    	string Contact ;
    	ifstream InputFile ;
    	
    	InputFile.open("ContactList.Txt") ; 
    	if(InputFile.fail() != true) {
    		InputFile >> Contact ;
    		while(InputFile.eof() != true) {
    			if(ContactList.empty()) {
    				ContactList.push_back(Contact) ;
    			} else {
    				i = 0 ;
    				while(i < ContactList.size() && ContactList[i] <= Contact) {
    					i = i + 1 ;
    				}
    				ContactList.insert(ContactList.begin()+i,Contact) ;
    			}
    			InputFile >> Contact ;
    		}
    		InputFile.close() ;
    		i = 0 ;
    		while(i < ContactList.size()) {
    			cout << ContactList[i] << endl  ;
    			i = i + 1 ;
    		}
    		cout << "Contact:" ;
    		cin >> Contact ;
    		while(Contact != "END") {
    			Pos = BinarySearch(Contact,ContactList) ;
    			if(Pos >= 0) {
    				cout << "Found at position " << Pos << endl ;
    			} else {
    				cout << "Not Found" << endl ;
    			}
    			cout << "Contact:" ;
    			cin >> Contact ;
    		}
    	}
    }
    
    
    int BinarySearch(string X, vector<string> List)
    {	int Left, Right, MidPoint ;
    	Left = 0 ;
    	Right = List.size() - 1 ;
    	MidPoint = (Left + Right) / 2 ;
    	while(Left <= Right && List[MidPoint] != X) {
    		if(X < List[MidPoint]) {
    			Right = MidPoint - 1 ;
    		} else {
    			Left = MidPoint + 1 ;
    		}
    		MidPoint = (Left + Right) / 2 ;
    	}
    	if(Left <= Right) {
    		return MidPoint ;
    	} else {
    		return -1 ;
    	}
    }
    
    


  • Banned (with Prison Access) Posts: 5,154 ✭✭✭Oriel


    Cheers.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    How about something a bit more simple:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    //function prototype
    void dostufftoword(string word);
    
    void main()
    {
    
    //file:
    string thefile = "myfile.txt";
    
    //Input file stream connection
    ifstream myfile;
    myfile.open(thefile.data());
    if (myfile.fail())
    {
    cerr << "Error opening file";
    exit(1);
    }
    
    for (;;)
    {
    string word;
    getline(myfile,word);
    
    //now here is where you can do some stuff, maybe pass it into a function
    dostufftoword(word);
       
    if (myfile.eof())
    break;
    }
    
    }
    
    void dostufftoword(string word)
    {
       //use this function to process your word
    }
    
    

    Think this is what you working on, amn't sure if that code is compilable, might contain errors but thats how i would approach it anyways.


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    If I was creating a dictionary style application, I'd be more inclined to use STL Maps as opposed to vectors.

    You said you want to apply some operation to each word, what sort of operation? If it's a case of just entering a definition for a word, then maps are your way to go.
    std::map < string, string> dictionary ;
    
    dictionary [ "chair" ] = "A piece of furniture that you can sit on" ;
    
    cout << "Chair:\n" << dictionary [ "chair" ] << endl ;
    
    if ( dictionary.find ( "table" ) == dictionary.end ( ) )
      cout << "Entry \"table\" NOT found!\n" ;
    


Advertisement