Problem in fgets(); function ?

This is a discussion on Problem in fgets(); function ? within the C Programming forums, part of the General Programming Boards category; 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); } ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    1

    Question 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!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Some comments, you shouldn't use fflush(stdin); it doesn't work. Also you call fgets with word two times, shouldn't one call include meaning? And, you can safely use 10 as string length in fgets since it reads one less character than specified in the length field.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    #1
    Code:
    fflush(stdin);
    fflush is undefined for input streams. It is only for output. It may happen to work but it is not guaranteed to always work as you intend.


    #2
    Quote Originally Posted by lionaneesh
    but i cannot use scanf();
    as it only inputs till a space but i want to input till newline..
    Says who:
    Code:
        char str1[100],str2[100] = {0};
        printf("Enter stuff:  ");
        fflush(stdout);
        scanf("%99s %99[^\n]", str1, str2);
        printf("First: \"%s\"\nSecond: \"%s\"\n",str1,str2);
    Output
    Code:
    Enter stuff: The quick brown fox jumped over the lazy dog.
    First: "The"
    Second: "quick brown fox jumped over the lazy dog."
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 12:17 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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