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 7971945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:44:37+00:00 2026-06-04T07:44:37+00:00

I’m doing a lot of iterations in order to simulate the bash command (Homework)

  • 0

I’m doing a lot of iterations in order to simulate the bash command (Homework) .

The code works great , but the problem is that after a few iterations of inputs , the program
starts to have some buffer problems . I suspect it has something to do with all the \n of Enter hits .

For example , the code starts like this :

#define BUFFER 4096
#define RUN_FOREVER 1
#define ERROR_SIGN -1
#define TRUE 1
#define FALSE 0

int main(int argc , char * argv[] )
{
    char input[BUFFER];

    //Get always a command line from the user.
    while(RUN_FOREVER)

    {

        if (isatty(0))
        {
            // input is from terminal
            // need to put something here 
        }

        char **separatedFormAmpersand ;

        int ampersandsCtr = 0, k=0,r=0;
        char *stringBeforeAmpersand = NULL;

        printf("$ ");


        memset(input, '\0', BUFFER);

         char ch;

        scanf("%[^\n]",input);
        scanf("%c",&ch);

        if(0 == strcmp(input, "exit"))
            break;

        //Separate the command according to the "&".
        stringBeforeAmpersand = strtok( input, "&");
... // more code (quite a lot , frankly) 

Now , if the user hits the following inputs :

ls Debug/ | grep r
ls >> file.jer & ls & ls & ls
ls >> file.jer

one after the other , then the code doesn’t recognize the ls command when I hit input number 3 .

If I enter each input in a single run of the code , everything works perfectly .

Any ideas how to clean the buffer ? maybe fflush ?

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-04T07:44:38+00:00Added an answer on June 4, 2026 at 7:44 am

    flushing stdout would clearly be a good thing to do before you can tell anything from your code.

    I assume that your pair of scanf has the following goal : one will grab one line, and the second will “skip the \n” character. You could just use getc() for the later, btw. The wonder is whether scanf() itself will read the \n character for you.

    In its GNU fashion, scanf() may prove superior to fgets() here, in that the %a[...] modifier can get you rid of “buffer size” limitations and lets the library malloc() a buffer of adequate size for you.

    Ever thought about invoking your program with strace to track reading of the input ?

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.