Problem in fgets(); function ?

Discussion in 'C' started by lionaneesh, Apr 1, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define count 2000000
    
    void add(char word[],char meaning[])
    {
        FILE *fp;
        fp = fopen("words.txt","a");
        fprintf(fp,"%s        =    %s\n",word,meaning);
    }
    
    void processing(void)
    {
    
        int i = 0,delay;
        printf("Processing....\n\n");        
        for(delay=0;delay<count;delay++)
                ;
        printf("\n");
    }
        
    
    void getwords(void)
    {
    
        FILE *fp;
        fp = fopen("words.txt","r");
        if(fp!=NULL)
        {
            printf("\n/**************************** DICITIONARY **********************************/\n\n\n");
            char words;
            while((words = fgetc(fp)) != EOF)
            {
                putchar(words);
            };
            fclose(fp);
        }
        if(fp=NULL)
        {
            printf("Unable to open the file!\n");
        }
            
    }
        
    int main()
    {
        int selection;
        char word[10];
        char meaning[40];
        printf("$$$$ Welcome to Aneesh's Dicitionary $$$$\n\n");
        printf("1. Show the dicitionary .\n");
        printf("2. Add words to dicitionary .\n3. Exit\n");
        printf("\tYour selection (1 , 2 or 3 [to quit]  ) : ");        
        selection=fgetc(stdin);
        if( selection ==  '1')
        {
            processing();        
            getwords();
        
        }
        else if( selection == '2')
        {
            processing();        
            printf("Enter word to add: ");    
            fflush(stdin);            
            fgets(word,9,stdin);
            fflush(stdin);            
            printf("Meaning : ");
            fflush(stdin);
            fgets(word,9,stdin);    
            add(word,meaning);
        }
            
    }
    In the above program in the main() function:-

    in the line no 67 and 71.

    what i m trying is to input is two character arrays :-
    1. word
    2. meaning

    but i cannot use scanf();
    as it only inputs till a space but i want to input till newline..

    problems :-

    1. the program only inputs the meaning and not the word.

    2. the output when we reach that code is" Enter word to add: Meaning:"
    and it will only input the meaning.

    Please help with the code so that it works as expected and inputs both the arrays correctly.

    Help!
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    see the changes below

    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define count 2000000
    [COLOR="Blue"]void getString(char *string,int buffer){//with this function we remove \n from the end of what fgets read ,if there is one.
        int i=0;
        fgets ( string, buffer, stdin );
        for ( i = 0; i < buffer; i++ ){
            if ( string[i] == '\n' ){
                string[i] = '\0';
                break;
            }
        }
    }[/COLOR]
    
    void add(char word[],char meaning[])
    {
        FILE *fp;
        fp = fopen("words.txt","a");
        fprintf(fp,"%9s=%40s\n",word,meaning);
        [COLOR="red"]fclose(fp);[/COLOR]//close the file
    }
    
    void processing(void)
    {
    
        int i = 0,delay;
        printf("Processing....\n\n");        
        for(delay=0;delay<count;delay++)
                ;
        printf("\n");
    }
        
    
    void getwords(void)
    {
    
        FILE *fp;
        fp = fopen("words.txt","r");
        if(fp!=NULL)
        {
            printf("\n/**************************** DICITIONARY **********************************/\n\n\n");
            char words;
            while((words = fgetc(fp)) != EOF)
            {
                putchar(words);
            };
            fclose(fp);
        }
        if(fp=NULL)
        {
            printf("Unable to open the file!\n");
        }
            
    }
        
    int main(){
        int selection=0;
        char word[10];
        char meaning[40];
       [COLOR="Red"] while (selection!=3){[/COLOR]//your menu needs a loop
            printf("$$$$ Welcome to Aneesh's Dicitionary $$$$\n\n");
            printf("1. Show the dicitionary .\n");
            printf("2. Add words to dicitionary .\n3. Exit\n");
            printf("\tYour selection (1 , 2 or 3 [to quit]  ) : ");        
            [COLOR="Red"]selection=(int)fgetc(stdin)-'0';[/COLOR]//convert char input to integer
            [COLOR="Red"]getchar();[/COLOR]//for garbage
    
            if( selection ==  [COLOR="Red"]1[/COLOR]){
                processing();        
                getwords();
            }else if( selection == [COLOR="Red"]2[/COLOR]){
                processing();        
                printf("Enter word to add: ");              
                [COLOR="Red"]getString(word,9);[/COLOR]          
                printf("Meaning : ");
                [COLOR="Red"]getString(meaning,39);[/COLOR]//error   here ,watch your copy paste!!!
                add(word,meaning);
           }
        [COLOR="Red"]}[/COLOR]   //loop ends here
    }
    
    
    
     
    Last edited: Apr 1, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice