 |
 |
No Foundation method for Terminal output?
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
Is the only way to read/write the terminal to use the old printf C tools? I'm surprised, but I can't find an NSInput/NSOutput anywhere...does it not exist?
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Originally posted by Gametes:
Is the only way to read/write the terminal to use the old printf C tools? I'm surprised, but I can't find an NSInput/NSOutput anywhere...does it not exist?
There aren't any classes for dealing with standard input or output. I guess they didn't see any reason to remake what was already there.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
I just wanted an easier way than printf(%d/n%$s="a",z); I actually don't know what I'm doing. I've never really written a command-line app before.
How do I get the entire line input as a string?
How do I output a string?
My past experience with this was very buggy, where no output would show till the end of the program. I would really appreciate a short sample, or directions to an Obc-C tutorial; I haven't been able to find one online yet.
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
I'm a living example of why Cocoa needs a dummy-proof tool for this.
I can't get all this low-level C stuff to work right.
My code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSString *a, *b;
printf("What's your problem? ");
scanf("%s", &a);
printf("\n", "ok");
scanf("%s", &b);
printf("whatever");
NSLog(@"Hello, World!");
[pool release];
return 0;
}
gives this output:
hi
What's your problem?
huh
2002-12-02 21:28:36.205 mCore[614] Hello, World!
whatever
mCore has exited with status 0.
Now, wouldn't "string = [NSStdIn get]" or [NSStdOut post:string] be easier?
Anyway, can someone help me get this right? I'm just trying to write a simple command-line tool.
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
You shouldn't need to do all that pools stuff!
The pool is managed automatically by the system.
Just send release to the object you want to release, and it will automatically decrement it's count in the pool and when that count gets to zero, the memory will be deallocated.
Ie, delete that line with the autorelease pool, and instead of [pool release], use something like
[a release]
and
[b release]
(if 'a' and 'b' are the objects you want to release to free up the memory they were using).
EDIT: oh, hang on, you're creating your own "main" function? You probably do want to manage your own pool then. Forget everything I just said. I always use the provided main function that comes in the Cocoa Application template.
(Last edited by Brass; Dec 2, 2002 at 09:22 PM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
First of all, scanf takes cString's as arguments, not NSString's. So your a's and b's should be char[] or char*'s. Second of all, the output looks fine here, so I don't know what's going wrong with your setup. What command line arguments are you using to compile this?
I don't think there are any STDTool class because the C functionality provides pretty much what you'll need. Any Objective-C wrapper would be uneccessary overhead. It would be trivial to write if you really wanted it.
Matt
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
I really appreciate your help with this. I realize it's a very newbie question, but I can't find the answer anywher (I spent ALL day looking).
Look, I don't have any attachment to the way my program looks above. It's just the way I had it set up, trying tomake it work. But it doesn't.
I just created a 'Foundation Tool' in PB, then added the stuff in main() where it says "add your code here". All the printf() is backwards compared to scanf(), that's my confusion (look at the code).
Please, humor me this: write the shortest main() which will CORRECTLY take input and print output to the terminal infinitely. I cannot make this happen, can you?
Input? oh god!!
ok oh god!!
Input? man
ok man
...
Thank you very much!
(Last edited by Gametes; Dec 2, 2002 at 11:16 PM.
)
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Ah! You're running this from PB. Here's the deal, from the Project Builder FAQ
Why isn't my program's output appearing in the Debugger Console?
On Mac OS X, _the stdio library buffers output dependent on what stdout is "connected" to. When stdout is connected to a terminal device (like a pseudo-terminal device which is used by Terminal), then it is line buffered; when stdout is connect to a file or a pipe(2) , it is block buffered. See the setbuf(3) man page for details.
In Project Builder, what the Executable's stdin/out is connected to depends on if it is Run or Debugged. When an Executable is Run, its stdin/out is connected to a pipe and when it is Debugged, it is connected to a pty (pseudo-terminal). This means the buffering behavior for an Executable's stdout varies when it is launch from within Project Builder.
Here's some sample code:
void log_to_stdout(char *msg)
{
___ printf("Log message: ");___ /* statement 1 */
___ printf("%s\n", msg);___ ___ /* statement 2 */
}
When debugging this code, after stepping over statement 1, the text from printf() will not be displayed; it is still in the stdout buffer because the text string did not contain a newline. After stepping over statement 2, you will see both strings of text output to the Console._ When Running, the output may not appear because stdout is block buffered. To flush the buffer, add a call to fflush() after statement 2.
Another example:
{
___ int____ i;
___
___ printf("Enter a number: ");___ ___ ___ ___ _/* statement 1 */
___ scanf("%d", &i);___ ___ ___ ___ ___ ___ _ _ /* statement 2 */
___ printf("%d * %d = %d\n", i, i, (i * i));___ /* statement 3 */
}
When debugging this code, the text for the first printf()_ will not be displayed after stepping over statement 1 because it does not contain a newline. However the call to scanf() will cause stdout to be flushed. However, when run, the call to scanf() will not automatically flush stdout because it is connected to a pipe. Add an explicit call to fflush(stdout) after statement 1 and statement 3.
For C++ code, there is currently a bug in the implementation of cout which causes data to be buffered when it should not be. A work-around is to call cout.sync_with_stdio(false)_ before sending any data to cout.
So, compile and run it from the command line like so:
gcc -o test.o test.m -framework Foundation
./test.o
Hope this helps,
Matt
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
Well that fixed that...screwy. Thank you.
On the down side, now I'm getting 'segmentation fault' errors after I input the string. Are you sure that printf/scanf is the way to go? There's no easier way to get strings to and from the command line? I find having to reference variable's addresses (via &var) and all that to be a bit too low-level...
Thanks; that was blowing my mind.
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by Gametes:
Well that fixed that...screwy. Thank you.
On the down side, now I'm getting 'segmentation fault' errors after I input the string. Are you sure that printf/scanf is the way to go? There's no easier way to get strings to and from the command line? I find having to reference variable's addresses (via &var) and all that to be a bit too low-level...
Thanks; that was blowing my mind.
Huh? Did you fix the problem where you were using NSString's when you want char*'s? I'll post some code when I get off work which will wrap this up for you, but it really isn't too bad.
Matt
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
I'm also getting this interesting behavior:
In[1]: 3
In[2]: 3 4
In[3]: In[4]: 3 4 5
In[5]: In[6]: In[7]: 3 4
In[8]: In[9]:
I've beaten it to death...there's no easier way? I just want the whole line input as a string.
Sorry to drag it out...man I like Cocoa so much more than this. 
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
This should work:
STDTool.h
Code:
#import <Foundation/Foundation.h>
@interface STDTool : NSObject
{
}
+ (NSString *)readString;
+ (int)readInt;
+ (double)readDouble;
+ (char)readChar;
+ (void)printString:(NSString *)string;
+ (void)printReturn;
@end
STDTool.m
Code:
#import "STDTool.h"
@implementation STDTool
+ (NSString *)readString
{
char array[255];
scanf("%s",array);
return [NSString stringWithCString:array];
}
+ (int)readInt
{
int a;
scanf("%d",&a);
return a;
}
+ (double)readDouble
{
double a;
scanf("%f",&a);
return a;
}
+ (char)readChar
{
char c;
scanf("%c",&c);
return c;
}
+ (void)printString:(NSString *)string
{
printf([string cString]);
}
+ (void)printReturn
{
printf("\n");
}
@end
I haven't tried everything out, but it looks good to me =D
Matt
(Last edited by Ghoser777; Dec 3, 2002 at 08:55 AM.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
Originally posted by Gametes:
Please, humor me this: write the shortest main() which will CORRECTLY take input and print output to the terminal infinitely. I cannot make this happen, can you?
Input? oh god!!
ok oh god!!
Input? man
ok man
...
Thank you very much!
Here's a very very simple version which probably won't handle multi-word strings:
Code:
main() {
char buffer[80];
while (1) {
printf("Input? ");
scanf("%s", buffer);
printf("ok %s\n", buffer);
}
}
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Sep 1999
Location: Ottawa, ON, Canada
Status:
Offline
|
|
Have you thought of using gets() instead of scanf? That will get the whole line and not the first word.
BTW,
char *buffer;
scanf("%s", &buffer);
will give a Seg Fault because you are referencing a pointer, or you have not allocated memory. Take out the '&' (because buffer is already a pointer) and you will be ok.
Also, don't forget to allocate memory for your buffer. e.g.
char *buffer;
buffer = (char *)malloc(NUMCHARS * sizeof(char));
gets(buffer);
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status:
Offline
|
|
Thank you all for your help. I sincerely appreciate it, and if I can ever get this stuff working hopefully I'll repay you by contributing something useful to the Mac community.
Is there anyway to avoid the char[finite size] limit? I don't want to presuppose how much the user will type.
|
|
you are not your signature
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2002
Status:
Offline
|
|
Yes, there is: read the input one character at a time into a dynamic collection object such as vector (from the c++ standard library) or NSArray. Much harder to do it that way, though 
|
|
[vash:~] banana% killall killall
Terminated
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status:
Offline
|
|
Originally posted by Gul Banana:
Yes, there is: read the input one character at a time into a dynamic collection object such as vector (from the c++ standard library) or NSArray. Much harder to do it that way, though
Or NSMutableString, which I think would be more convenient (unless you want to start parsing as the characters come in, which you may want to do).
Should I add that to my class?
Matt
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|