Solved

stdin and stdout - type any length of string

Posted on 2010-09-15
7
428 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now