stdin and stdout - type any length of string

I want to be able to have the user type in any amount of characters on the screen and then exit when they hit EOF (cntrl-d).

I have tried using getchar which works well but I can't get the characters from the screen.  Aren't the characters the user is typing located in a stdin or stodout file somewhere?  I'd like to be able to access them.

JOe K.
char c = getchar();
while((c = getchar() != EOF){}

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You don't have balanced parenthesis.Also your c char should be an int.Also, you may not want to initialize c in your definition since the while loop fills in the first char on the first pass. (But that is application dependent.)int c;   while( (c = getchar() ) != EOF){}Note that EOF is an int.
ClaudeWalkerAuthor Commented:
Sorry, I copied and pasted wrong.  My solution was a shot in the dark.

I guess what I'm after is to use fgets(someBuffer, sizeof(someBuffer), stdin) to grab undetermined user inputted strings line by line from the stdin?

Is there a way I can write to the screen and access those characters (or ints) from stdin?

JOe K.
Try this -
int c=0;
while(c != EOF)
   c = getc();

Note: use Ctrl+Z to exit.
You can enter as many characters on the console. All of them will be read for every 'Enter' key press.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

>> what I'm after is to use fgets(someBuffer, sizeof(someBuffer), stdin) "If the End-of-File is encountered and no characters have been read, the contents of str remain unchanged and a null pointer is returned. If an error occurs, a null pointer is returned. Use either ferror or feof to check whether an error happened or the End-of-File was reached." is a code snippet modified from this fgets link just to check for stopping condition (i.e., EOF or error).
   char mystring [100];
   while( fgets (mystring , 100 , stdin) != NULL ) {
      puts (mystring);

Open in new window

If you start writing C++ code, you can use one of these getline() functions:

You can try this approach as well. Working with string in C++ has benefits over the C-style string. I'll be back tomorrow if you wish to discuss this further.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ClaudeWalkerAuthor Commented:
Apparently I would have to use "pipes" as a technique to read this (stdin or stdout) buffer.  

Regarding my "solution" is was far from elegant.  I ended up writing the text into a file which I was trying to avoid because it seemed redundant.  Now that I know how to use pipes I'll be able to do this for endevors.

Thanks again,
JOe K.

>> I would have to use "pipes" as a technique to read this (stdin or stdout) buffer.  
I must be misunderstanding your original question. Would you like to elaborate a little on exactly what you are trying to accomplish? Why are you saying pipes are necessary?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.