Solved

stdin and stdout - type any length of string

Posted on 2010-09-15
7
430 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

776 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