Write File with NSData
Write File with NSData
- Subject: Write File with NSData
- From: Aalok <email@hidden>
- Date: Mon, 12 Jun 2006 20:29:16 +0530
Hi everyone!!
I want to write a file whose output should be
^C^@^@^@&^@^@^@{2081F9DF-481D-40A3-8203-7B37C5299A27}^O^@^@^@ClientConnected^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];
const int *sizeOfMsgKind=1;
const int *sizeOfTotalStr=3;
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.
my C program for your ref
#include<stdio.h>
int writeBinaryInteger(const void *inData, FILE *outFile,char commandString[]);
int main(int argc, char* argv[])
{
int totalElements = 3;
int sizeOfGUID = 38;
int sizeOfCommand;
int sizeOfMsgKind = 1;
char fileName[32];
//char GUID[38];
char *GUID;
char commandString[32];
char msgKind[1];
FILE *outFile;
printf("argc = %d", argc);
printf("argv = %s", argv[0]);
printf("\nargv[1] --->>>> %s\n", argv[1]);
GUID = argv[1];
//strcpy(GUID, argv[1]);
strcpy(commandString, argv[2]);
strcpy(msgKind, argv[3]);
strcpy(fileName, argv[4]);
printf("\nGUID --->>>> %s\n", GUID);
sizeOfCommand = strlen(commandString);
if(!(outFile = fopen(fileName,"w")))
return 1;
//Write the totalElements
writeBinaryInteger((char*)&totalElements, outFile,commandString);
fseek(outFile, sizeof(int), SEEK_SET);
//Write Size of GUID
writeBinaryInteger((char*)&sizeOfGUID, outFile,commandString);
fseek(outFile, sizeof(int) * 2, SEEK_SET);
printf("\nGUID --->>> %s\n", GUID);
//Write GUID
fwrite(GUID, strlen(GUID), 1, outFile);
fseek(outFile, ((sizeof(int)*2)+strlen(GUID)), SEEK_SET);
//Write size of the command
writeBinaryInteger((char*)&sizeOfCommand, outFile,commandString);
fseek(outFile, ((sizeof(int)*3)+strlen(GUID)), SEEK_SET);
//printf("commandString --> %s strlen(commandString) --> %d",
commandString, strlen(commandString));
//Write command string
fwrite(commandString, strlen(commandString), 1, outFile);
fseek(outFile, ((sizeof(int)*3)+strlen(GUID)+strlen(commandString)), SEEK_SET);
//Write size of the message kind
writeBinaryInteger((char*)&sizeOfMsgKind, outFile,commandString);
fseek(outFile, ((sizeof(int)*4)+strlen(GUID)+strlen(commandString)), SEEK_SET);
//Write message kind
fwrite(msgKind, strlen(msgKind), 1, outFile);
fclose(outFile);
return 0;
}
int writeBinaryInteger(const void *inData, FILE *outFile, char commandString[])
{
FILE *tempFile;
int binaryInteger[4];
char tempFileName[50];
strcpy(tempFileName, "temp");
strcat(tempFileName, commandString);
printf("\n <<writeBinaryInteger>> ");
//Wrire the integer to the temp file in binary format
tempFile = fopen(tempFileName, "w");
//tempFile = fopen("tempFile", "w");
fwrite(inData, sizeof(int), 1, tempFile);
fclose(tempFile);
//Open the temp file and read the binary integer one byte at a time
//in the reverse order. This is done due to difference in the way files
//are written on mac and windows. The aim is to write the file in the format
//that can be read on windows.
tempFile = fopen(tempFileName, "r");
//tempFile = fopen("tempFile", "r");
fread(&binaryInteger[3], 1, 1, tempFile);
fseek(tempFile, 1, SEEK_SET);
fread(&binaryInteger[2], 1, 1, tempFile);
fseek(tempFile, 2, SEEK_SET);
fread(&binaryInteger[1], 1, 1, tempFile);
fseek(tempFile, 3, SEEK_SET);
fread(&binaryInteger[0], 1, 1, tempFile);
fseek(tempFile, 4, SEEK_SET);
//Close the temp file
fclose(tempFile);
//Now the write the bytes to the outFile
fwrite((char*)&binaryInteger[0], 1, 1, outFile);
fwrite((char*)&binaryInteger[1], 1, 1, outFile);
fwrite((char*)&binaryInteger[2], 1, 1, outFile);
fwrite((char*)&binaryInteger[3], 1, 1, outFile);
}
_______________________________________________
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