Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

stdin and stdout - type any length of string

Posted on 2010-09-15
7
Medium Priority
?
436 Views
Last Modified: 2012-05-10
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.

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

Open in new window

0
Comment
Question by:ClaudeWalker
  • 4
  • 2
7 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 33688748
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.
0
 

Author Comment

by:ClaudeWalker
ID: 33688793
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?

Thanks,
JOe K.
0
 
LVL 5

Expert Comment

by:xdomains
ID: 33688854
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Expert Comment

by:phoffric
ID: 33689555
>> 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."    http://www.cplusplus.com/reference/clibrary/cstdio/fgets/Here 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

0
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 33689593
If you start writing C++ code, you can use one of these getline() functions:
     http://www.cplusplus.com/reference/string/getline/
     http://www.cplusplus.com/reference/iostream/istream/getline/

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.
0
 

Author Comment

by:ClaudeWalker
ID: 33735659
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.

0
 
LVL 32

Expert Comment

by:phoffric
ID: 33736365
>> 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?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question