Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help! -- Getting input from terminal while having file input redirection.

Help! -- Getting input from terminal while having file input redirection.
Thread Tools
Junior Member
Join Date: Oct 2001
Location: Los Angeles
Status: Offline
Reply With Quote
Feb 14, 2002, 07:04 PM
 
So I'm new to programming on the UNIX environment (a student) and am having trouble with the terminal. I am working in ProjectBuilder, however it needs to be able to compile on a Sun. (Oh, and it's a command-line app.)

My application is supposed to be able to work with both keyboard input and file redirection from the command-line (ie "program <file.in"). At a certain point during the execution of the program, I am supposed to wait for the user to hit enter/return at the terminal no matter where the input stream is coming from. My professor gave use the following code for this (in C):

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
void WaitForReturn()
{
printf(<font color = red>"\n(hit ENTER/RETURN to continue...)"</font>);
#ifndef WIN32
FILE *fp;
if((fp = fopen(<font color = red>"/dev/tty"</font>,<font color = red>"r"</font>)) == NULL)
{
fprintf(stderr, <font color = red>"\ncould not open /dev/tty\n"</font>);
exit(<font color = blue>5</font>);
}
fgetc(fp);
fclose(fp);
#else
char buf[<font color = blue>3</font>];
buf[<font color = blue>0</font>] = '\<font color = blue>003</font>';
_cgets(buf);
#endif
}
</font>[/code]

Before I go on:
1) My code is in C++, but the above C code compiles fine.
2) I left out the Mircosoft stuff in my actual code but have it here for the rest of you. Do I need to do something like this for Apple though?

Anyhow, when I compile and run the above code, that code alway causes program to exit with with error 5. -- That is, when the file opening fails.

What is going on exactly? I can figure out as much that a file stream is trying to be opened off of my terminal... but that's about it. I understand tty is supposed to return the current terminal or something???

So, any ideas? As always, any help you give me is useful and thanks in advance.

[ 02-14-2002: Message edited by: Arakageeta256 ]
     
Junior Member
Join Date: Oct 2001
Location: Los Angeles
Status: Offline
Reply With Quote
Feb 14, 2002, 07:19 PM
 
Well I figured out part of the problem-- it has something to do with Project Builder.

The code works as it should when I run the compiled app from Terminal-- not Project Builder's terminal. But how do I get Project Builder's to work? Is there some temporary change I can make to the above code so that I can get the same functionality in Project Builder? (I would change it back once the program was completed).

Thanks again.
     
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Feb 16, 2002, 12:56 AM
 
Arakageeta256,

How about this?

<font face = "courier">#include &lt;iostream.h&gt;

void main()
{
cout &lt;&lt; "please press return..";
cin.get();
cout &lt;&lt; "done." &lt;&lt; endl;
}</font>

This worked for me on the command line and with an input file. Hopefully it also works on a Sun.

--Juggle5
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:41 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2