Solved

stdin and stdout - type any length of string

Posted on 2010-09-15
7
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

763 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