Re: how to stop NSString/NSData from interpreting backslash
Re: how to stop NSString/NSData from interpreting backslash
- Subject: Re: how to stop NSString/NSData from interpreting backslash
- From: Angela Brett <email@hidden>
- Date: Wed, 1 Oct 2003 21:39:54 +1200
Problem is, I am reading a source file and doing some analysis, and the
source file has lots of backslashes. I don't want those interpreted as
escape chars, I want to literally see the \ chr(92) and then read the
next char.
This should not be a problem when you're actually reading from a
file. I just ran your code as it was and got the results you
described, but when I changed the line to this:
NSString* src = [NSString
stringWithContentsOfFile:[@"~/Scrap/thing.txt"
stringByStandardizingPath]];
where the file referred to contains:
abc\n\ndef
I got the output
0 a
1 b
2 c
3 \
4 n
5 \
6 n
7 d
8 e
9 f
So I think the problem you are seeing is just caused by the way you
are testing it and wouldn't be a problem in the real application.
I thought maybe NSString was "helping me out", so I read in
the file as NSData...
NSData* source = [NSData dataWithContentsOfFile:filename];
char* p = (char*)[source bytes];
but p+x still evaluates the \ followed by an n as a carriage return
rather than two bytes of data, chr(92) followed by chr(110).
It's odd that you had the same problem reading NSData from a file,
because I didn't have the problem with this code (based on your code
snippets):
NSData *source=[NSData dataWithContentsOfFile:[@"~/Scrap/thing.txt"
stringByStandardizingPath]];
char* p = (char*)[source bytes];
for (x = 0; x < [source length]; x++)
{
printf("%i %c\n", x,p[x]);
}
--
Angela Brett
email@hidden
http://macintosh.geek.nz
_______________________________________________
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.