Objective-C BufferedReader
Objective-C BufferedReader
- Subject: Objective-C BufferedReader
- From: Hamish Allan <email@hidden>
- Date: Sat, 12 Feb 2005 22:39:44 +0000
Hi,
I have as yet been unable to find anything written in Objective-C that
provides the functionality of Java's java.io.BufferedReader.
1) Does such a library object exist, and if so, where?
Assuming not, I thought I might try to implement it naively as below
(readLine hard-coded to stdin for pedagogical purposes). It works fine
for strings that are less than BUFFER_LENGTH characters long, but if
they are over, fgets() never returns. I know this is not really a Cocoa
question, but since I was asking (1) above, I thought I'd also ask
here:
2) What's wrong with the following code?
- (NSString *)readLineFromStdin
{
NSMutableString *ret = [NSMutableString
stringWithCapacity:BUFFER_LENGTH];
char buf[BUFFER_LENGTH + 1];
char *chars = fgets(buf, BUFFER_LENGTH, stdin);
if (chars != NULL)
{
char *endl = strchr(chars, '\n');
while (endl == NULL)
{
[ret appendString: [NSString stringWithCString:buf
length:BUFFER_LENGTH]];
endl = strchr(chars, '\n');
}
[ret appendString: [NSString stringWithCString:buf length:(endl -
buf)]];
}
return ret;
}
Many thanks,
Hamish
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden