Thread: Fgets and pointer

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Thumbs down Fgets and pointer

    I am just trying to learn C, and now I am learning about pointer and string.!!!

    since the number of array elements in C cannot be increased at run-time, i am trying to write a test script like below.

    1. first, let a user enter the number of a string
    2. reserve memory for the length of the string
    3. get user input
    4. correctly output the string to the screen
    however, the code below did not work as I expect! the output is something unreadable!!!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {
    	char *myVar;
    	int numberOfChar;
    
    	printf("What is the length of your string: ");
    	scanf("&#37;i", &numberOfChar);
    
    	//
    	myVar = (char *) malloc(numberOfChar * sizeof(char));
    	printf("\nPlease enter your string: \n");
    
    	fgets(myVar, numberOfChar*1, stdin); //something error!! I guess
    	//scanf("%s", myVar);
    
    	printf("you entered:\n%s", myVar);
    
    
    	printf("\n\n");
    	return ;
    }
    Last edited by whichet; 11-21-2007 at 12:39 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I expect the problem is after the call to scanf() a newline character is left in the input stream. This newline character is read by the subsequent fgets(). So you could clear the input stream after the scanf():
    Code:
    	printf("What is the length of your string: ");
    	scanf("&#37;i", &numberOfChar);
    	while (getchar() != '\n');
    Some other comments about your code:
    (1) main() returns an int and should be defined as such.
    (2) There should be no cast on the return value of malloc().
    (3) You should malloc numberofChar + 1 to make room for the string terminator.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Echo swoopy. It's probably scanf's fault. If you are going through the trouble of learning how fgets() works, then it is probably best to abandon using scanf and start using fgets() all the time. They treat whitespace entirely different from each other. For example.

    Say your program runs like this:
    What is the length of your string: 5
    Please enter your string: // no time to type!
    you entered:

    What happened here? Well, when scanf finally encountered the newline character after you finished typing "5<enter>" then the scanf function probably put back the '\n', maybe like this:

    ungetc( last_char, _File );

    Here's why.

    So you have an unread, undiscarded \n character in your stream. fgets, like scanf, terminates when it sees the \n character. The difference is, that character is put into the string if there is room. If we ran the same program with a small change in printing the output:
    What is the length of your string: 5
    Please enter your string: // no time to type!
    you entered: "
    "

    Now you may recognize the problem.

    Here's one thing you can do about it! Read a number the smart way. Use fgets and sscanf ( or a similar function ):
    Code:
    int readint( void )
    {
       int num;
       char line[512]; /* or big enough */
       if( fgets( line, sizeof line, stdin ) != NULL )
         if( sscanf( line, "%d", &num ) != 1 ) num = EOF; /* EOF means the conversion failed */
       return num;
    }
    Last edited by whiteflags; 11-21-2007 at 01:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 09-11-2008, 03:31 PM
  2. Replies: 4
    Last Post: 04-21-2008, 12:15 PM
  3. fgets changes my pointer!
    By 9988776655 in forum C Programming
    Replies: 2
    Last Post: 12-30-2007, 03:06 AM
  4. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  5. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM