Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem in fgets(); function ? (http://www.go4expert.com/forums/fgets-function-t21620/)

lionaneesh 1Apr2010 22:18

Problem in fgets(); function ?
 
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!

virxen 2Apr2010 02:22

Re: Problem in fgets(); function ?
 
see the changes below

Code:

#include<stdio.h>
#include<string.h>

#define count 2000000
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;
        }
    }
}


void add(char word[],char meaning[])
{
    FILE *fp;
    fp = fopen("words.txt","a");
    fprintf(fp,"%9s=%40s\n",word,meaning);
    fclose(fp);//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];
  while (selection!=3){//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]  ) : ");       
        selection=(int)fgetc(stdin)-'0';//convert char input to integer
        getchar();//for garbage

        if( selection ==  1){
            processing();       
            getwords();
        }else if( selection == 2){
            processing();       
            printf("Enter word to add: ");             
            getString(word,9);         
            printf("Meaning : ");
            getString(meaning,39);//error  here ,watch your copy paste!!!
            add(word,meaning);
      }
    }  //loop ends here
}



All times are GMT +5.5. The time now is 15:36.