read a number with scanf() and read char[] with fgets()

This is a discussion on read a number with scanf() and read char[] with fgets() within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> int main() { int n; char s[30]; scanf("%d",&n); fgets(s,sizeof(s),stdin); printf("n=%d s=%s",n,s); getch(); return 0; } ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    71

    read a number with scanf() and read char[] with fgets()

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    int main()
    {
    	int n;
    	char s[30];
    	scanf("%d",&n);
    	fgets(s,sizeof(s),stdin);
    	printf("n=%d s=%s",n,s);
    	getch();
    	return 0;
    }
    s[] skips reading,and s[] has only "\n"
    how avoid this?(s I want to also contain spaces,so read s[] with scanf is not a solution)

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    It's best avoided by not mixing scanf() with fgets().

    Read everything into a buffer using fgets().

    Then use say sscanf to extract what you need, and validate what was input (not that sscanf is particularly good at validating)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    I read almost the entire article posted Bayint Naung and I thought about this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    int main()
    {
    	int n;
    	char s[30],mess[2];
    	scanf("%d",&n);
    	fgets(mess,sizeof(mess),stdin);
    	fgets(s,sizeof(s),stdin);
    	printf("n=%d s=%s",n,s);
    	getch();
    	return 0;
    }
    read a another char[] only for mess...so mess[] keep the "\n" ,skip read mess[] and jump to read s[] (sorry for my english)
    is good code?

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That is fairly pointless, and as Salem suggested the easiest way is to avoid scanf here. Generally scanf is a bad function to use when you are getting input from the user and not from a predefined input file. The user can type in anything(see the Infinite monkey theorem) and particularly something that scanf can't handle properly.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you insist on mixing them, simply pay attention to your return values:
    Code:
    if( fscanf( place, "%d", &number ) == 1 )
    ... yay a number
    else
    if( fgets( buf, bufsize, place ) )
    ... yay stuff
    else
    ... a fail is you
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read until negative number
    By m_user in forum C Programming
    Replies: 10
    Last Post: 04-03-2009, 06:38 AM
  2. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 03:02 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. replacing scanf with fgets
    By subflood in forum C Programming
    Replies: 6
    Last Post: 08-19-2004, 01:59 PM
  5. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM

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