Comparing words in a file...

This is a discussion on Comparing words in a file... within the C Programming forums, part of the General Programming Boards category; Hey guys. I'm in the stages of learning C. I'm making a program for practice. What I want the program ...

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

    Comparing words in a file...

    Hey guys. I'm in the stages of learning C. I'm making a program for practice. What I want the program to do is open a file, then compare all the words inside of it, then display what words that were used more than once. Well, I'm stumped. I've opened the file, and I read all the contents of it, and it can display each word. But, I'm not sure how to go about comparing each word. I think what I have to do is get them inside strings and use strcmp(), but I'm having some trouble. This is my code:

    Code:
    int main()
    {
    	char ifilename[] = "c:/test.txt";
        char test;
        
    	FILE *fp;
    
    	/* Opens the file, and displays an error if it can't */
        if((fp = fopen(ifilename,"r")) == NULL) {
    		printf("Couldn't open for reading.\n");
    		exit(1);
    	}
    
    	/* Reads all the contents inside the file */
        while((fscanf(fp,"%s", test)) != EOF) {
    	printf("%s\n", test);
    	}
    
    	fclose(fp);
    
    	return 0;
    }
    I tried to place each word on an individual line, and then compare each line, but I couldn't get that to work. How would I place each word inside a string?

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Research strcmp() and try thinking it out. Solve the problem first before translating it to code. If you have good problem solving skills, you will be a much better programmer in not only C, but any language you want.
    Do not make direct eye contact with me.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, test is not a string. It's a single character. Don't you pay attention to what your compiler tells you? It should be screaming warnings at you telling you you're using the fscanf call wrong. Anyway...

    If you plan on pulling words out and then keeping track of every single word so you can count how many times it's used, consider something like a linked list. Smash the case of every word, because otherwise it's going to be a pain in the ass, and then do something like:
    Code:
    loop
        read a word into a buffer
        smash the case of the buffer
        check the list to see if the word is in the buffer
            if so
                increment that bucket's word counter
            if not
                add this word to the list, set it's counter to one
    end loop when you're done with the file
    Something like that would be a fairly painless way to do it.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    Quzah, I originally had it as "char test[30];" instead, but before I pasted the code there I was trying to get it to work a different way and forgot to change that back.

    Thanks for the quick responses, guys. I'll look into what you both said.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Quote Originally Posted by quzah
    ...screaming...
    ...Smash...
    ...pain in the...
    Are you having a bad day Quzah?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21