Thread: Strcmp

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Strcmp

    I want to create a program that asks the user to input a word ,
    then it searches the word in the given txt file. So far what i have been able to do is ,
    i have used the getline function, to display the contents , then i have converted the std::string line
    to c-string to get tokens ,now i want to compare these tokens with the word that the user entered,
    but i dun know how.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdio.h>
    #include <string.h>
    
    using namespace std;
    
    
    
    int main ()
    {   
      	char * 			  cstr;
    	char * 			  p;
    	string 			  line;
    	fstream 		  myfile;
    	char delim[]	  ="  :,.-;!?_-()";
    	char 			  arr[12];
    	
    	cout<<"Enter a word to check whether it is in the text file:";
    	cin>>arr;
    
    
    
    
    
    	
    	myfile.open ("Ahoo.txt");
    
      		if (myfile.is_open())
    			
    			{
     				  		
    				while (!myfile.eof())
      		
    					{
    					
    						getline (myfile,line);
    						cstr = new char [line.size()+1];	
    						strcpy (cstr, line.c_str());											
    						p=strtok (cstr,delim);													
      							
    							while (p!=NULL)
     					    	       {
        							cout << p << endl;
        							p=strtok(NULL,delim);
    						 	}	
    
     						delete[] cstr;  
    				
    				   	}
    				
    				myfile.close();
    			}
    
    			else 
    			{
    				cout<<"File not opened";
    			}
      return 0;
    }

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Are you just trying to get the job done (quick n dirty) or shooting for a more elegant solution?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    I ll be thankful , if you tell me both of the ways.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well two of the many ways of skinning this cat: (sips tea and wishes for IV):
    Quick n Dirty:
    1. Get word from user.
    2. mmap file
    3. for(position = 0; position < (fileSize - wordSize); position++)
    4. if(memcmp(word, filePtr+position, wordSize)==0)
    5. print "Match found, position is ", position

    However if the quick n dirty solution isn't what your instructor is striving for then prepare to get your knuckles rapped. A more elegant search solution can be found here:

    http://en.wikipedia.org/wiki/Boyer–M...arch_algorithm

    However that may be what we call a 100 dollar solution to a 10-buck problem. It helps to know what your actual goal is.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You shouldn't mix C-strings with C++ std::string. Not that it is wrong, but why not use all the functionalities of std::string?

    First of all, you can just use find() and do your job really quickly.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    string str ("There are two needles in this haystack with needles.");
      string str2 ("needle");
      size_t found;
    
      // different member versions of find in the same order as above:
      found=str.find(str2);
      if (found!=string::npos)
        cout << "first 'needle' found at: " << int(found) << endl;
    i was going through this example , but i didn't get the highlighted part of the code.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    myfile.open (filename."txt");

    I want the user to tell the file name , which the program will open , there is some error with this statement. if anyone can tell me how to write this.

  8. #8
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    I think everything you need to understand that code is on this page. If you read the whole page...
    Asking a question you already know the answer to might teach you something you did not know...

  9. #9
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Quote Originally Posted by Fatima Rizwan View Post
    myfile.open (filename."txt");
    Code:
    myfile.open ("filename.txt");
    You can use a variable in place of the file name
    Asking a question you already know the answer to might teach you something you did not know...

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Fatima Rizwan View Post
    Code:
    string str ("There are two needles in this haystack with needles.");
      string str2 ("needle");
      size_t found;
    
      // different member versions of find in the same order as above:
      found=str.find(str2);
      if (found!=string::npos)
        cout << "first 'needle' found at: " << int(found) << endl;
    i was going through this example , but i didn't get the highlighted part of the code.
    string::npos is just a constant integer defined somewhere (in the string class actually, that is why it starts with string:.
    If find() doesn't find what you want, then found == string::npos. That is all.

    As for open, indeed you can use a variable. Of type const char*. You could do this for example
    Code:
    cin >> str;
    myfile.open(str.c_str());

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Ok, thanks
    Now if the given string is found in the file. I want to print all the attributes of that file.
    Is there any function for this purpose?Any help!
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    #include <io.h>
    #include <time.h>
    
    using namespace std;
    
    class FileManipulator
    {	
    		string word;
    		string name;
    
    	public:
    
    		void wordinput()
    		{
    			cout<<"Enter word:";   	cin>>word;
    		}
    
    		void nameinput()
    		{
    			cout<<"Enter file:";	cin>>name;
    		}
    
    		string getname()
    		{
    			return name;
    		}
    
    		void manipulation()
    		{
    			fstream  my_file;		
    			string line;
    			int found;			
    			string ext=".txt";
    			
    
    			my_file.open (getname()+ext);
    			{
    				if (my_file.is_open())
    			
    				{
    				
    					while (!my_file.eof())
    					{
    		
    						getline (my_file,line);
    						found=line.find(word); 
    							
    						if (found != string::npos)
    						{	
    							cout<<found;
    						}
    						else 
    						{
    							cout<<"NOT found";
    						}
    					}
    					my_file.close();
    				}
    
    
    				else 
    				{
    					cout<<"File not opened";
    				}
    			}		
    		}
    
    		/*void printFile()
    			   
    		}*/
    
    };
    
    int main()
    {
    	FileManipulator f;
    	f.wordinput();
    	f.nameinput();
    	f.manipulation();
    	/*f.printFile();*/
    	return 0;
    }

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while (!my_file.eof())
    {
    
        getline (my_file,line);
    
        ...
    
    }
    This method of controlling loops by testing eof is error prone and rarely done correctly. It is recommended to test the read operation directly, getline in this case:
    Code:
    while ( getline(my_file,line) )
    {
    
        ...
    
    }



    Code:
    string getname()
    {
        return name;
    }
    
    void manipulation()
    {
        ...
    }
    Member functions that do not modify the class should be const:
    Code:
    string getname() const
    {
        return name;
    }
    
    void manipulation() const
    {
        ...
    }



    Code:
    int found;			
    
    ...
    
    found=line.find(word); 
    							
    if (found != string::npos)
    The find member function returns a value of type size_type:
    Code:
    string::size_type found;
    
    ...
    
    found=line.find(word); 
    							
    if (found != string::npos)


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    ...
    #include <time.h>
    Prefer the newer versions of these header files where available:
    Code:
    #include <cstdio>
    #include <cstdlib>
    ...
    #include <ctime>


    Code:
    fstream  my_file;		
    ...
    string ext=".txt";
    
    ...
    			
    my_file.open (getname()+ext);
    The fstream object's open member function does not accept string arguments (currently). You need to use the c_str member function to convert the string container into something it does accept. (Has this changed yet?)
    Code:
    my_file.open ((getname()+ext).c_str());
    "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

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    I have made the amendments now if I have found the word required in file, i want to output its attributes. like file name, its location , time it was creatd , its size etc. Kindly help me with this.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What OS are you using? There are several options. A possible Windows/Microsoft solution could use functions such as GetFileAttributes. For other OS's others would need to point you to appropriate functions.
    "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

  15. #15
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Look up stat() and friends if you are on a *NIX OS; POSIX has a whole raft of ops that can answer these questions...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM