1 Replies - 4922 Views - Last Post: 08 December 2011 - 10:32 PM

#1 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Object C Writing NSMutableData to file

Posted 01 December 2011 - 01:51 AM

OK. I'm attempting to pass a NSMutableArray to NSMutableData and then append another NSMutableData to that one and then write that to a file. I know this sounds like a lot of extra work for simply writing an array to a file... I just couldn't come up with a better way to do it. The problem is I can't get the NSMutableData to append. And I don't want to write an array to file in plist(xml) format.


Richard


int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        //File Managment
    NSString* fileName    = @"SongList.txt";
    NSString* homeDir    = NSHomeDirectory();
    NSString* fullPath = [homeDir stringByAppendingPathComponent:fileName];
    
        //Data passing file allocation
    NSMutableData* buffer1 = [[NSMutableData alloc] init];
    NSMutableData* buffer2 = [NSMutableData dataWithData:buffer1];
    
        //Arrays
    NSMutableArray *songArray = [[NSMutableArray alloc]init];
    NSArray *songName = [NSArray arrayWithObjects:@"Layla",@"Bridge Over Troubled Water",@"Let It Be",@"Your Song",@"Get Up (I Feel Like Being A) Sex Machine",@"Lola",@"Who'll Stop the Rain",@"Fire and Rain",@"Paranoid",@"All Right Now", nil];

        //Data manipulation
    for (int i=0; i<10; i++) {
        [songArray addObject:[songName objectAtIndex:i]];
        buffer1 = [songArray objectAtIndex:i];
        [buffer2 appendData:buffer1];
    }
        
        //Write array to file
    [buffer2 writeToFile:fullPath atomically:YES];
    
        //Memory Managment
    [buffer2 release];
    [buffer1 release];
    [pool drain];
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Object C Writing NSMutableData to file

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2984
  • Posts: 10,316
  • Joined: 08-August 08

Re: Object C Writing NSMutableData to file

Posted 08 December 2011 - 10:32 PM

This works for me:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        //File Managment
    NSString* fileName    = @"SongList.txt";
    NSString* homeDir    = NSHomeDirectory();
    NSString* fullPath = [homeDir stringByAppendingPathComponent:fileName];
    
        //Data passing file allocation
    NSString* buffer1 = [[NSString alloc] init];
    NSString* buffer2 = [[NSString alloc] init];
    
        //Arrays
    NSMutableArray *songArray = [[NSMutableArray alloc]init];
    NSArray *songName = [NSArray arrayWithObjects:@"Layla",@"Bridge Over Troubled Water",@"Let It Be",@"Your Song",@"Get Up (I Feel Like Being A) Sex Machine",@"Lola",@"Who'll Stop the Rain",@"Fire and Rain",@"Paranoid",@"All Right Now", nil];
    buffer2 = @"";
        //Data manipulation
    for (int i=0; i<10; i++) {
        [songArray addObject:[songName objectAtIndex:i]];
        buffer1 = [songArray objectAtIndex:i];
        
        buffer2 = [buffer2 stringByAppendingString:buffer1];
        buffer2 = [buffer2 stringByAppendingString:@"\n"];
    }

    //Write array to file
    [buffer2 writeToFile:fullPath atomically:YES encoding:NSUnicodeStringEncoding error:nil];

    //Memory Managment
    //[buffer2 release]; pool drain makes these superfluous. 
    //[buffer1 release];
    [pool drain];
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1