Solved

stdin and stdout - type any length of string

Posted on 2010-09-15
7
427 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

708 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

19 Experts available now in Live!

Get 1:1 Help Now