reading input from stdin
reading input from stdin
- Subject: reading input from stdin
- From: pmcurry <email@hidden>
- Date: Mon, 10 Nov 2003 20:55:46 -0800
Using Foundation tool I did my assignment (a Go game) following the
specs which said:
"Input: All input will come from standard input. The first line of
input will be a single integer, either 9, 13, or 19, specifying the
size of the board. Subsequent input will consist of consist of either
the coordinates of an intersection or the word pass. The coordinates
of an intersection are given using a letter, specifying the column, and
a number, specifying the row. There will be no space between the letter
and the number. The letter can be either uppercase or lowercase."
Using:
// scan
NSData* inputData = [[NSFileHandle fileHandleWithStandardInput]
availableData];
NSString* inputString = [[NSString alloc] initWith
Data:inputData
encoding:NSASCIIStringEncoding];
// cleanup the string
if ( [inputString characterAtIndex:[inputString length] - 1] == '\n'
)
inputString = [inputString substringToIndex:[inputString length]
- 1];
return inputString;
This works if I sit at the terminal and enter the input. But the prof
is testing it by redirecting an input file into the app. The result is
that 'availableData' takes the whole file in one gulp and errors out
trying to read the 2nd line.
It looks like I could deal with the redirected input file by using an
NSScanner approach, but it looks like that wouldn't work if I wanted to
sit at the terminal and enter the moves individually. Is that true or
not? If true, what would be an approach that handles both cases?
Thanks.
-Phil
_______________________________________________
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.