Thread: Seg faults. structs. pointers.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    Seg faults. structs. pointers.

    I'm having trouble with this program. it reads in a list of words from a text file and allows you to add words, search for words, compare 2 lists and print the list. the first line of the text file contains the language of the words, and the second line contains the number of words. the following lines contain the words. like so:

    Code:
    english
    14
    abjure
    abrogate
    abstemious
    acumen
    antebellum
    auspicious
    belie
    bellicose
    bowdlerize
    chicanery
    chromosome
    churlish
    circumlocution
    circumnavigate
    im getting a problem when i try to fscanf a value into the num_words variable in the structure. i get a seg fault error there.

    another problem im getting is where i let the for loop in open_file() run until MAX_WORDS... it gets crazy( line 76).
    i wrote the program to print the words read in so i can see that it was reading the file OK. when it runs till MAX_WORDS it prints all kinds of crazy characters and stuff. but if i change it to a->num_words it wont run. i know it has something to do with the pointers but i dont know how to fix it. ive spent like 3 hours trying to figure it out.

    also in that function there is the fscanf for a->lang and a->num_words. i have them commented out because i was trying to just increment a->num_words(line 78) this solved the first problem but didnt solve the following problem.

    and lastly, in the add_word() function when i add the words to a->b[num_words + 1] i get another seg fault.

    please let me know if you can help.

    here is the code
    Code:
    # include <stdio.h>
    # include <string.h>
    
    # define MAX_WORDS      20
    
    struct wordlist {
           char lang[15];
           int num_words;
           char b[MAX_WORDS][21];
    };
    
    void    open_file(struct wordlist *a);
    void    add_word(char word[21], struct wordlist *a);
    int     contains(char word[21], struct wordlist a);
    void    display_word_list(struct wordlist a);
    
    int main() {
        struct  wordlist a;
        char    word[21];
        int     c=0, sel, i=0;
                
        open_file(&a);       
    	
    		printf("Option 1\tAdd A Word\n");    
    		printf("Option 2\tContains\n");
    		printf("Option 3\tEqual Lists\n");
    		printf("Option 4\tDisplay List\n");
    		printf("Option 5\tExit\n");
    		scanf("%d", &sel);
    		
            if (sel ==1) {            
                printf("please enter a word to add: \n");
                scanf("%s", &word); 
                add_word(word, &a);
                printf("SUCCESS\n");
                for( i = 0; i <= MAX_WORDS; i++ ) {                        
                    printf("%s\n", a.b[i]);   
                }
            }
                    
            else if (sel ==2) {
                printf("Enter a word to search for: \n");
                scanf("%s", &word);
                c = contains(word, a);
                if( c == 1) 
                    printf("Word Found"); 
                else if ( c == 2) 
                    printf("Word Not Found"); 
            }
                            
            else if (sel ==3) {
                    
            }
            else if (sel ==4) {
                    
            }
            else if (sel ==5)
                return(0); 
            
            
        
    system("pause");
        return(0);
    }
    
    void open_file(struct wordlist *a){
        
        FILE    *ifp;
        int     i=0, j=0;
        
            ifp = fopen("input.txt", "r");
            //fscanf(ifp, "%s", a->lang);
            //fscanf(ifp, "%d", a->num_words);
            
            for( i = 0; i <= MAX_WORDS; i++ ) {                 
                fscanf(ifp, "%s", a->b[i]); 
    			a->num_words++;         
            }
            for( i = 0; i <= MAX_WORDS; i++ ) {                        
                    printf("%s\n", a->b[i]);   
            }
    	
    		fclose(ifp);
    }
    
    void add_word(char word[21], struct wordlist *a) {
    
        int     i=0, j;  
        
    		
            if ( a->num_words == MAX_WORDS ) {
                printf("List full, word not added.");
            }
            else {
                for ( i = 0; i < a->num_words; i++) {
                    if(strcmp(a->b[i], word)==0) {
                        break;
                    }
                    else {
                        printf("check");
                        strcpy(a->b[a->num_words+1], word);
                        //printf("check");
                    }
                }
            }
              
    }
    
    int contains(char word[21], struct wordlist a) {
        int    i=0;   
         
        for ( i = 0; i <= a.num_words; i++) {
            if (strcmp(a.b[i], word)==0) {
                return(1);
            }
            else {
                return(2);  
            }                
        }
    }
    
    
    /*
    void equal_lists() {
         
         
         
                  
    }
    
    void display_word_list(struct wordlist a) {
         int i=0;
         
             for (i=0; i<a.num_words;i++) {
                 printf("%s\n", a.b[i]);
             }
         
         
         
    }
    */
    i just realized that contains() doesent work either. but dont bother trying to figure it out unless it just jumps out at you.

    thanks a lot for the help.
    Last edited by ominub; 05-02-2009 at 09:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2009, 09:29 PM
  2. Confused with pointers and structs
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 10-12-2008, 02:22 PM
  3. It seg faults right away
    By Brimak86 in forum C Programming
    Replies: 6
    Last Post: 01-31-2008, 10:40 PM
  4. Seg Faults at Random
    By mrpickle in forum C Programming
    Replies: 5
    Last Post: 01-15-2004, 03:42 PM
  5. pointers and structs
    By alexir in forum C Programming
    Replies: 0
    Last Post: 10-20-2001, 01:28 AM