Thread: Comparing a text file with a dictionary file help!!

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    Comparing a text file with a dictionary file help!!

    Hi, i'm trying to compare two text files..one is a user created file and it needs to be compared with a dictionary file. It is then supposed to output which word if any is spelled wrong and display which line number. Can someone guide me in what to do, and maybe supply me some code? thanks!

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    bool word_search(string, string);
    bool bad_word(string, int, ostream&);
    
    
    
    int main(){
    	ifstream ins;
    	ofstream outs;
    		string outFile;
    		string inFile;
    		string line;
    		int lineCount;
    		lineCount = 0;
    	
    		cout << "Please enter an output filename: ";
    		cin >> outFile;
    		
    		cout << "Please enter an input dictionary file: ";	
    		cin >> inFile;
    		
    			
    			
    				
    				
    	word_search(outFile, inFile);
    			
    	return 0;
    }
    bool word_search(string out, string in){
    	ifstream ins;
    	ifstream ins2;
    	ofstream outs;
    	ofstream outs2;
    	string line;
    	int lineCount;
    	lineCount = 0;
    	
    
    	
    			ins.open(in.c_str());
    				if(ins.fail()){
    					cout << "***Error: Cannot open " << in
    						<< 	" for input." << endl;
    					return EXIT_FAILURE;
    				}
    				
    			ins2.open(out.c_str());	
    				if(ins2.fail()){
    					cout << "***Error: Cannot open " << out
    						<<  " for output." << endl;
    					return EXIT_FAILURE;
    				}	
    				
    				
    		
    			getline(ins, line);
    			while(!ins.eof()){
    				lineCount++;
    				cout << line << endl;
    				outs << line << endl;
    				getline(ins, line);
    			}
    			
    			
    		
    	
    			getline(ins2, line);
    			while(!ins2.eof()){
    				lineCount++;
    				cout << line << endl;
    				outs2 << line << endl;
    				getline(ins2, line);
    			}
    			
    		
    				
    			
    			return 0;
    		
    	
    		
    	}

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Which bit are you actually having trouble with? Remember, the more precise your question is, the more precise the answers will be.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    well with the code i have right now, it displays me the words in both files, but I don't know how to actually compare the strings in both files to each other so as to check and see if a word is spelled correctly

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    stl::set might be of some use. Depending on the format of your dictionary then
    Code:
    std::set<std::string> dict;
    std::ifstream dict_stream(dict_filename.c_str());
    for(std::string word;dict_stream >>word;) dict.insert(word);
    might be all you need to do to parse your dictionary. if dict.count(word) == 0. then the word never appeared in the dictionary.
    std::getline(std::istream,std::string,char); will get a line from a file
    now all you need is to write a function to split a string into two strings, one containing the first word without any whitespace and another containing the rest of the string. a function to convert a string to all lower case might not be a bad idea either.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    And just how are you thinking of trying to determine if a word is simply not in your dictionary or rather mispelled? I.e. if the first word in your check file is "pail" and your first word in the dictionary file is "apple" how do you make a determination that this is obviously not the word you are trying to check versus spitting out an "error: incorrect spelling of apple message". You need to come up with a set of rules (a heuristic) that can make that distinction. Now, if you have that rule system in place you can exclude "apple" and move on to the next word in the dictionary. Let's say your heuristic gets you up to "pain" in your dictionary file and that "pail" happens to not exist in your current version of the dictionary file. Your heuristic should probably in this case see that "pail" and "pain" are very similar and make the suggestion "did you mean pain". You rule system can become quite complex.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I suggest using a data structure called a trie - it is excellent for storing words and making various operations and checks.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM