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

  • SEARCH
  • Home
  • 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 9142323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:45:43+00:00 2026-06-17T09:45:43+00:00

I am using the following method clear the input buffer: void dump_line(FILE *fp) {

  • 0

I am using the following method clear the input buffer:

void dump_line(FILE *fp) {
    int ch;    
    while ( (ch = fgetc(fp)) != EOF && ch != '\n');
}

This works fine if I read from stdin using scanf():

char string[51];
...
scanf("%50[^\n]", string);
dump_line(stdin);

However, if I use fgets() instead, as in the following code:

char string[52];
...
fgets(string, 52, stdin);
dump_line(stdin);

// Since fgets() reads in the '\n', trim the end of the string:
string[strlen(string)-1] = 0;

Since fgets() reads in the ‘\n’ character I found the need to allocate a buffer one char larger and trim the string back one character. This works fine but there is a problem with the dump_line function. When using the scanf() version, it works as expected since there will always be a ‘\n’ left in the input buffer due to the use of [^\n]. So even if that is the only char left in the buffer, in dump_line() the fgetc() reads the char and since it is a ‘\n’ the while loop is broken.

In the fgets() version, the ‘\n’ is read in if it will fit in the string buffer, leaving stdin empty. This is causing the program to wait on the fgetc(), requiring the user to hit the enter key to progress any further, which is obviously not what I am after. On the other hand, if the user enters a string too long for the buffer, the dump_line() is working fine!

Im guessing the problem is something to do with EOF, but I cannot seem to get this to work as intended! Any help would greatly be appreciated.

If it is any help the platform I am working on is Mac OSX, just in case this is some kind of platform-specific quirk.

  • 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-17T09:45:43+00:00Added an answer on June 17, 2026 at 9:45 am

    Check whether you need to clear the buffer,

    fgets(string, 52, stdin);
    size_t len = strlen(string);
    if (string[len-1] == '\n') {
        // read in whole line, no need to clear buffer
        string[len-1] = 0;
    } else {
        // junk left in buffer, clear it
        dump_line(stdin);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following this advice to clear a FILE input in IE (specifically using the
I'm using the following method to open my SQLite db. private void open() {
We are using the following method to write the log to log file. The
I want to clear my textarea field using following method. Here is my code
The following method, based on code in this question , shows a file download
I am using following method to get bitmap from url but image is not
I am using the following method to reverse geocode a google maps latlng: [GClientGeocoder.getLocations(address:String,
I am using the following method to basically create a JSON string. var saveData
I am using the following method to get the time of the video with
I'm using the following method in my code: - (NSMutableArray *) newOrderedArray:(NSMutableArray *)array ByKey:(NSString

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.