FROM : Bill Bumgarner
DATE : Fri Jul 21 18:01:20 2006
On Jul 21, 2006, at 8:04 AM, PGM wrote:
> In my app, I have a document containing a potentially huge two-
> dimensional C-array filled with doubles that I need to write to a
> file (with a user-specified number of decimals). My original plan
> was to use NSDocument's "dataRepresentationOfType" and fill an
> NSMutableString with the text and use that. However, this takes
> forever because the NSMutableString has to allocate additional
> memory again and again ("initWithCapacity" did not help much).
>
> What I did then is to use "writeToFile:ofType:" and then use plain
> C to write the data to a file:
>
> FILE *writing;
> double **matrix = [myData distanceMatrix];
> const char *out_name;
> out_name = [fileName cString];
> writing = fopen(out_name, "w");
> fprintf(writing, everything); //looping over the matrix
>
> This works, but I am afraid that it will break when the fileName
> contains non-ASCII characters. Is there another fast way to write a
> large C-array to a file that does work with non-ASCII filenames?
(Others have pointed out not to use cString, etc..)
You are writing binary data to a file. It isn't a string and,
thus, NSMutableString is completely inappropriate for the task at hand.
You can use NSFileHandle:
+ (id)fileHandleForWritingAtPath:(NSString *)path
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSFileHandle_Class/Reference/Reference.html#//apple_ref/occ/
clm/NSFileHandle/fileHandleForWritingAtPath:
Followed by a series of calls to writeData:
- (void)writeData:(NSData *)data
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSFileHandle_Class/Reference/Reference.html#//apple_ref/occ/
instm/NSFileHandle/writeData:
Of course, you will need to get your double* arrays into an instance
of NSData for writing. You will want to avoid copying the data in
doing so:
+ (id)dataWithBytesNoCopy:(void *)bytes length:(unsigned)length
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/clm/
NSData/dataWithBytesNoCopy:length:
DATE : Fri Jul 21 18:01:20 2006
On Jul 21, 2006, at 8:04 AM, PGM wrote:
> In my app, I have a document containing a potentially huge two-
> dimensional C-array filled with doubles that I need to write to a
> file (with a user-specified number of decimals). My original plan
> was to use NSDocument's "dataRepresentationOfType" and fill an
> NSMutableString with the text and use that. However, this takes
> forever because the NSMutableString has to allocate additional
> memory again and again ("initWithCapacity" did not help much).
>
> What I did then is to use "writeToFile:ofType:" and then use plain
> C to write the data to a file:
>
> FILE *writing;
> double **matrix = [myData distanceMatrix];
> const char *out_name;
> out_name = [fileName cString];
> writing = fopen(out_name, "w");
> fprintf(writing, everything); //looping over the matrix
>
> This works, but I am afraid that it will break when the fileName
> contains non-ASCII characters. Is there another fast way to write a
> large C-array to a file that does work with non-ASCII filenames?
(Others have pointed out not to use cString, etc..)
You are writing binary data to a file. It isn't a string and,
thus, NSMutableString is completely inappropriate for the task at hand.
You can use NSFileHandle:
+ (id)fileHandleForWritingAtPath:(NSString *)path
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSFileHandle_Class/Reference/Reference.html#//apple_ref/occ/
clm/NSFileHandle/fileHandleForWritingAtPath:
Followed by a series of calls to writeData:
- (void)writeData:(NSData *)data
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSFileHandle_Class/Reference/Reference.html#//apple_ref/occ/
instm/NSFileHandle/writeData:
Of course, you will need to get your double* arrays into an instance
of NSData for writing. You will want to avoid copying the data in
doing so:
+ (id)dataWithBytesNoCopy:(void *)bytes length:(unsigned)length
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/clm/
NSData/dataWithBytesNoCopy:length:
| Related mails | Author | Date |
|---|---|---|
| PGM | Jul 21, 17:04 | |
| Dado Colussi | Jul 21, 17:27 | |
| Tommy Nordgren | Jul 21, 17:44 | |
| Douglas Davidson | Jul 21, 17:54 | |
| Bill Bumgarner | Jul 21, 18:01 | |
| Uli Kusterer | Jul 21, 18:21 | |
| Steve Bird | Jul 24, 03:29 | |
| Bill Bumgarner | Jul 24, 04:26 | |
| PGM | Jul 24, 04:40 | |
| Jakob Olesen | Jul 24, 10:15 | |
| PGM | Jul 24, 15:55 | |
| Michael Ash | Jul 24, 17:39 |






Cocoa mail archive

