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 ]