Re: Write File with NSData
Re: Write File with NSData
- Subject: Re: Write File with NSData
- From: "A.M." <email@hidden>
- Date: Mon, 12 Jun 2006 12:28:42 -0400 (EDT)
The problem is that you don't understand that Obj-C is a strict superset
of C. Thus, '*' does not denote an object and ints are not objects- see
below:
On Mon, June 12, 2006 10:59 am, Aalok wrote:
> Hi everyone!!
>
>
> I want to write a file whose output should be
>
>
> ^C^@^@^@&^@^@^@{2081F9DF-481D-40A3-8203-7B37C5299A27}^O^@^@^@ClientConnec
> ted^A^@^@^@1
>
> The format is
>
>
> 4bytes = int in binary
> 4bytes = int in binary
> GUID in string
> 4bytes = int in binary
> string 4bytes = int in binary
> string
>
> I am using following function, but it doesnt work and I am totally
> confused whats going behind.
>
> -(void)generateMetaDataFile:(NSString *)fileName
> GUID:(NSString *)GUID
> command:(NSString *)commandStr
> msgKind:(NSString *)msgKind
>
>
> {
> NSMutableData *data1, *data2, *data3, *data4, *data5, *data6, *data7;
>
>
> /*This part give the warning: initialization makes pointer from
> integer without a cast*/ const int *sizeOfGUID=38; const int
> *sizeOfCommandStr=[commandStr length];
***Don't ignore warnings.
> const int *sizeOfMsgKind=1; const int *sizeOfTotalStr=3;
***This is most certainly not what you mean- here, you are creating three
const int pointers just like in C. There should be no difference here
between the Obj-C and the C versions. In Cocoa, objects are generally
stored on the heap, so we use pointers to references them.
Also, your code below shows that you don't understand how mutable data
objects work- specifically, they can adjust their space as needed, so all
you need to do is append to one NSMutableData object.
>
> const char *utfGUID = [GUID UTF8String]; const char *utfCommandStr =
> [commandStr UTF8String];
> const char *utfMsgKind = [msgKind UTF8String];
>
> unsigned char *aBuffer; unsigned len;
>
> data1 = [NSMutableData dataWithBytes:sizeOfTotalStr length:4];
>
> data2 = [NSMutableData dataWithBytes:sizeOfGUID length:4]; data3 =
> [NSMutableData dataWithBytes:utfGUID length:strlen(utfGUID)];
>
>
> data4 = [NSMutableData dataWithBytes:sizeOfCommandStr length:4]; data5 =
> [NSMutableData dataWithBytes:utfCommandStr
> length:strlen(utfCommandStr)];
>
>
> data6 = [NSMutableData dataWithBytes:sizeOfMsgKind length:4]; data7 =
> [NSMutableData dataWithBytes:utfMsgKind length:strlen(utfMsgKind)];
>
>
>
> len = [data2 length]; aBuffer = malloc(len);
>
> [data2 getBytes:aBuffer];
> [data1 appendBytes:aBuffer length:len];
>
>
> len = [data3 length]; aBuffer = malloc(len);
>
> [data3 getBytes:aBuffer];
> [data1 appendBytes:aBuffer length:len];
>
>
> [data1 writeToFile:fileName atomically:NO];
>
>
> actually I have written the program in C but don't understand how to write
> the same thing in obj-C?
>
> Please help. I am in deep trouble because of this.
>
>
> Thank you in advance.
_______________________________________________
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