About the stdin buffer

Contribute code & patches
Forum rules
No support questions here please

About the stdin buffer

Postby nemax on Wed Nov 30, 2011 1:32 am

I`m stuck in a progranming with c in linux!

when we open a shell,a character picture will be presented!

But before the picture show we can also input some characters,and after the picture shows,the characters you have input will be presented again to tell you that you have input sth already.

how can I do this with c?

just like in other linux and I open a shell,I input some characters(here is "ls") before the PS1 environment viriable shows,and finally I get this:

ls nemax@nemax-laptop:~$ ls

the red ls is my input before the "nemax@nemax-laptop:~$" show,and the blue ls is handle by system right?the system move my input(the red ls) to the end.

How can i write a function like this to handle the red ls?
Level 1
Level 1
Posts: 1
Joined: Wed Nov 30, 2011 1:12 am

Linux Mint is funded by ads and donations.

Re: About the stdin buffer

Postby xenopeek on Wed Nov 30, 2011 4:44 pm

That is the default if you use stream functions like getc or gets. Type in terminal:
Code: Select all
man getc

To read more about these functions.

Basically, getc reads 1 character from the stdin stream. If you type characters before your program is ready to accept input, they get buffered in the stdin stream and you can read them later with functions like getc (or the other functions that work on streams). You can also using functions like fflush, to clear a stream, so there are no buffered characters. Useful if you want to ask the user to enter a password (you don't what mistaken press keys earlier to end up in the input for that).
User avatar
Level 23
Level 23
Posts: 18173
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: About the stdin buffer

Postby DrHu on Wed Nov 30, 2011 5:02 pm

You might also want to check out string processing programs to see how they does it !
http://www.tenouk.com/clabworksheet/cch ... tring2.pdf
--you can probably find your own samples/examples..
User avatar
Level 17
Level 17
Posts: 7549
Joined: Wed Jun 17, 2009 8:20 pm

Return to Code & Patches

Who is online

Users browsing this forum: No registered users and 2 guests