Re: Corrupted NSMutableString
Re: Corrupted NSMutableString
- Subject: Re: Corrupted NSMutableString
- From: Dustin Voss <email@hidden>
- Date: Mon, 4 Aug 2003 14:57:34 -0700
On Saturday, August 2, 2003, at 07:30 AM, Matthew wrote:
--- Cameron Hayne <email@hidden> wrote:
On 28/7/03 4:08 PM, "Matthew" <email@hidden> wrote:
I'm working on a simple program that reads in data from a
binary file, processes the information, and copies it to a
plain text file (for processing in MATLAB). I can read all of
the data in fine. I can even print it without a problem. I'm
reading the data and appending it to a NSMutableString so that
I can use writeToFile to save the new text file. For some
strange reason, the NSMutableString gets corrupted. Only the
last two lines are important at the moment. The printf works
fine, but the appendFormat randomly chokes. (I've checked the
values by hand using a hex editor.)
short int * zeroCalibrations[*numOfChannels];
short int * gainCalibrations[*numOfChannels];
short int trial[*numOfChannels];
[snip]
...
NSMutableString * matlabFile = [[NSMutableString alloc]
initWithString:@""];
... various loops ...
[dataFile getBytes:trial range:NSMakeRange(*headerLength +
cellLength + (q * ((*numOfChannels * 2) *
*numOfSampPointsPerOb)) + (r * (*numOfChannels * 2)), 2 *
*numOfChannels)];
That is a complicated range expression. It would be better to put
it into separate variables, which you then could print out to
check that you are not asking for more data than will fit in the
array 'trial'.
And I think you are treading on thin ice by using [NSData
getBytes] to read short integers. You seem perhaps to be relying
on the byte order and that sizeof(short int) is 2 ? Better not to
build such dependencies into your code.
Since the binary file's definition uses 2-byte short integers (for this
section), I'm kind of stuck. Since C defines short integers as being
two bytes, it "should" not be a problem. (The byte order is specified
in the file, but I don't know how to swap the bits if I had to.) I have
stopped using the getData though. I've just added another loop so that
I'm just reading in one number at a time rather than a whole row of
numbers.
short * tempShort = [dataFile bytes] + offSet);
NSNumber * trialValue = [[NSNumber alloc]initWithShort:*tempShort];
I've stepped through the code. Everything is coming in fine.
Int16 *tempShort = (Int16 *)[dataFile bytes] + offset;
NSNumber *trialValue = [[NSNumber alloc] initWithShort:
CFSwapInt16LittleToHost(*tempShort)];
or CFSwapInt16BigToHost(), if the bytes are big-endian. This eliminates
any uncertainty.
... more loops ...
printf("%.3f", (trial[s] - (float)*zeroCalibrations[s]) * 400 /
*gainCalibrations[s]);
[matlabFile appendFormat:@"%.3f ", (trial[s] -
(float)*zeroCalibrations[s]) * 400 / *gainCalibrations[s]];
You know that "(trial[s] - (float)*zeroCalibrations[s]) * 400"
truncates the trial to an integer, right? For more accuracy, you might
want to explicitly cast everything to float like this:
(float)trial[s] - (float)*zeroCalibrations[s]) * 400.0f /
(float)(*gainCalibrations[s])
And am I right in thinking that gainCalibrations is a array of
pointer-to-floats?
After appending the 2472143rd float (-10.342), to the end of
matlabFile the string gets erased and the 2472144th element
(-17.289) becomes the first item. If I just put a blank space
after the %.3f, then the first string that I try to write comes
out ok, but the the second string gets truncated at a different
spot.
At this moment, I'm at a loss. I've stepped through the file
while checking all the variables. Everything goes fine until
the string gets erased. The string should end up being about
30MB, but gets cut down to 10.9MB. The following strings get
processed without a problem. If I make a simple change to what
get appended, then the location of the problem moves.
It seems likely that you have some problem relating to not
allocating enough space for your arrays and hence overwriting
something.
And anyway, why accumulate this all into one string if all you
are going to do is write it to a file? Easier to read some data,
format it, and write it out to the file before going on to read
the next piece of data.
I would LOVE to write directly to a file, but I've had a few problems:
- How do you get NSString writeToFile: to append rather than overwrite
the existing file?
- I tried using a NSMutableArray, but when you writeToFile is saves the
data as a XML file. (170MB at that...)
- I've tried to use NSFileHandle writeData:, but it requires NSData. I
couldn't get the NSData encoding to write plain ASCII...
NSString *trialValueString = [[NSString stringWithFormat:@".3f ",
(float)trial[s] - (float)*zeroCalibrations[s]) * 400.0f /
(float)(*gainCalibrations[s])];
NSData *trialValueData = [trialValueString
dataUsingEncoding:NSASCIIStringEncoding];
[trialFileHandle write
Data:trialValueData];
_______________________________________________
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.