Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:41:20+00:00 2026-06-16T04:41:20+00:00

Possible Duplicate: How do you allow spaces to be entered using scanf? How can

  • 0

Possible Duplicate:
How do you allow spaces to be entered using scanf?
How can I scan strings with spaces in them using scanf()?

New to C and I feel this should be very simple but for some reason I’m not seeing the clear answer.

Trying to write a name and address to a file but if I put ‘FirstName LastName’ only FirstName gets entered into the file. Same with address. Is there a way to accept the whole name with the space into the file?

struct person{
     char name[20];
     char address[50];
     char telno[20];
} info;

fp=fopen("contacts","a")

printf("Enter Name : ");
scanf("%s",info.name);

printf("Enter Address : ");
scanf("%s",info.address);

fprintf(fp,"%20s %20s %20s",info.name,info.address,info.telno);
fclose(fp);

New to C so please excuse my ignorance. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:41:21+00:00Added an answer on June 16, 2026 at 4:41 am

    The only way to read “white space” in to a string with scanf() is with the negated scanset:

    // This will read into comment everything up until the newline
    scanf(" %[^\n]",info.name); 
    

    In general the answer you’re going to get however is to “not use scanf” and use fgets() instead:

    fgets(info.name, sizeof info.name, stdin);
    

    Don’t forget a lot of this info for scanf() and fgets() and others is easy to get with a quick search.


    Edit: I tried replacing scanf with fgets but when I run the program it skips past the "Enter Name:"

    If it’s “skiping” by your fgets() that means that your fgets() just read a left over character that was sitting on the stdin buffer. I’d wager a guess it was a newline character.

    When you enter a string:

    john smith
    

    You’re getting a newline '\n' appended to the end after the 'h'. That means scanf() is going to read the letters and leave the newline, fgets() on the other hand will read everything up to/including the newline. So if you were to do this:

    scanf("%s",info.name);
    
    fgets(info.address, sizeof info.address, stdin);
    

    The fgets() would appear to be “skipped”. If you’re saying replacing both with fgets() is skipping the first one, then your stdin buffer isn’t clean and you must have something before hand leaving a newline character on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do you allow spaces to be entered using scanf? char inputStr[64];
Possible Duplicate: HTML Text Input allow only Numeric input I used this script function
Possible Duplicate: How can I only allow certain filetypes on upload in php? i'm
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Jquery validation - allow number without the leading zero? I am using
Possible Duplicate: AuthComponent: Difference between allowedActions and allow()? What is the difference between using
Possible Duplicate: How to allow only numeric (0-9) in HTML inputbox using jQuery? I
Possible Duplicate: event handles & visible I am new to learning WebRequest.This is my
Possible Duplicate: Allow php sessions to carry over to subdomains How to setup user
Possible Duplicate: alloc, init, and new in Objective-C I am a little confused about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.