Program I/O
I've built an MPW tool that prints out "Enter a number: " and then reads the number. But, when I type a number and press ENTER, the program reads the entire line instead of just the number. What's wrong?
It's due to the way the MPW Shell handles Standard Input. If the current selection in a window is an insertion point (i.e. nothing is selected), then the entire contents of the line containing the insertion point is sent to Standard Input. If you have selected some text in the window, only the contents of the selection is passed to Standard Input. You can avoid this problem by always ending your prompts with a newline ('\n') character so that any input can be entered on its own line. Alternatively, you can build your program as an SIOW application instead of an MPW tool. The SIOW library works a little differently, in that if the insertion point is left on the same line as the last text output, only the text from the insertion point to the end of the line is passed to Standard Input. If you move the cursor to another line, or select some text, then SIOW behaves like the MPW Shell.
I've built an SIOW application that uses cout to print out a string as follows
cout << "Hello World!";
but the string never appears. What's wrong?
When using cout within an SIOW application, you should terminate each line of output with an endl to ensure that the buffer is flushed properly. Your line of code should look like the following:
cout << "Hello World!" << endl;
I've built an application that uses printf() to output data but the data ends up in a file named "stdout" instead of on the screen. Why?
The printf() and scanf() functions operate on the predefined stdout and stdin file streams. In a stand-alone Macintosh application, these streams are implemented as physical files in the Mac's file system. Thus, anything written to stdout will end up in a file named "stdout". Conversly, if you have a file named "stdin" in the same directory as the application, the contents of that file will be used to satisfy any read requests to the stdin stream. If you want the more traditional implementation of file streams, you should build your program as an SIOW application or as an MPW tool.