Re: Problem with 'scanf'
Re: Problem with 'scanf'
- Subject: Re: Problem with 'scanf'
- From: "William Moss" <email@hidden>
- Date: Thu, 26 Sep 2002 15:48:24 -0400
>
I don't know if it's the right place to post this,
>
but can someone tell me why this in Project Builder
>
is not running as it should. Thanks
When I put in your code into a Project Builder project (named "testscanf")
and press the "Build and run active executable" button in the toolbar,
Project Builder displays a compile window (which executes its function fine
and without warnings). It then displays a window titled "Run testscanf -
(testscanf)". It shows a large "Stop" sign but no output in the window.
If I run a simple "Hello World" application, like:
#include <stdio.h>
int main (int argc, const char *argv[])
{
printf("Hello, World!\n");
return (0);
}
pressing the "Build and run active executable" button will display "Hello,
World!" followed by "hello has exited with status 0." and the Stop sign
will have disappeared.
To my understanding, the difference is that the Run window isn't a full
terminal. It's merely putting the content of standard output and standard
error onto the screen (which is why you see the Hello World and exited
messages) but since it has no way to handle standard input, a program using
scanf will appear to hang forever while it waits for input that isn't
coming. Why doesn't it at least display the prompt to "Enter a number" from
the printf statement before the scanf? I think it's because (in the
interest of efficiency) the Run window is buffering data.
In my searches I haven't found a way to tell project builder to run the
program via the terminal, but it's still not hard to test it for yourself
in the Terminal app. (It's just a bit more work on your part.)
To run the code, I open the project, enter the code, and build it (with the
hammer icon) but I DON'T run it inside Project Builder. I then launch
Terminal and when a shell prompt appears I change directories to my project
(in this case I put it in my a folder named "dev" in my home directory)
cd ~/dev/testscanf
Doing an ls in that directory shows several source files and other stuff,
but now that I've built the project there should be a directory named
"build" I change into that directory and list the files...
cd build
ls
You'll probably see two things the actual executable and the executable's
build directory with Project Builder related stuff. In this case you want
to run the executable, but if you just type the name (testscanf) it will
give you the response "Command not found" since the file isn't installed in
your default path. You have to explicitly tell it to run the program IN THE
CURRENT DIRECTORY by putting a period and slash in front of the filename.
./testscanf
Your program should then work fine, asking you for the numbers and taking
your input.
I realize that this can be a bit tedious, but you really only have to do
this once while debugging your program. At this point, leave your terminal
window open, with the directory set to the build directory, then once you
change and build your project again, you just go back to terminal and type
./testscanf again. Even easier you can probably just press the up arrow key
and the last command you typed will appear. So eventually you will just get
in the habit of doing Command-Tab to switch to the terminal, up arrow, and
return to run the program.
If anyone else out there knows a way to get ProjectBuilder to spawn a real
terminal window to execute a program that uses the standard input stream,
I'd love to hear it.
Thanks,
William
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.