将C指针的内容存储到具有Objective-C的文件中

将C指针的内容存储到具有Objective-C的文件中,objective-c,openssl,Objective C,Openssl,我有一个CMS\U内容信息指针 CMS_ContentInfo *encryptedDataWithCMS = CMS_encrypt(certList, dataToEncrypt, cipher, NULL); 。。。我想将CMS_内容信息存储到一个新的.p7m文件中。 有人知道怎么做吗 编辑:此外,我需要将其保存在objective-c(NSObject的任何实例)中 谢谢, Chris不确定这是否是您的要求,但要写出结构的内容: void writePtr( CMS_Conte

我有一个CMS\U内容信息指针

    CMS_ContentInfo *encryptedDataWithCMS = CMS_encrypt(certList, dataToEncrypt, cipher, NULL);
。。。我想将CMS_内容信息存储到一个新的.p7m文件中。 有人知道怎么做吗

编辑:此外,我需要将其保存在objective-c(NSObject的任何实例)中

谢谢,
Chris

不确定这是否是您的要求,但要写出结构的内容:

void writePtr( CMS_ContentInfo *tehPtr ){

    FILE *tehFile;

    //opens the file you want to write in, please notice the fopen mode "wb".
    tehFile = fopen( "tehFilePath", "wb" );
    //write 1 block of sizeOf( CMS_ContentInfo ) bytes into tehFile with the contents
    //of tehPtr
    fwrite( tehPtr, sizeOf( CMS_ContentInfo ), 1, tehFile );

    fclose( tehFile );

}

CMS_ContentInfo readPtr(){

    FILE *tehFile;
    CMS_ContentInfo *tehPtr;

    //opens the file you want to read from, please notice the fopen mode "rb".
    tehFile = fopen( "tehFilePath", "rb" );
    //allocates tho pointer where the read data will be stored.
    tehPtr = (CMS_ContentInfo *) malloc( sizeOf(CMS_ContentInfo) );

    //reads 1 block of sizeOf( CMS_ContentInfo ) bytes from tehFile into tehPtr
    fread( tehPtr, sizeOf( CMS_ContentInfo ), 1, tehFile );

    fclose( tehFile );

    return tehPtr;

}

部分是的,但我需要它在目标c中:)那是我的问题。我有一个c指针,需要将其“转换”为NSObject(或继承的实例),以使用Apple API调用保存它。(我的意思是用objective-c语法保存它,以防止使用fopen、fwrite等手动保存时出现问题。)@Chris:你可以使用whitelionV的技术将它保存到
NSMutableData
中,而不是写入文件,然后你可以随心所欲地保存数据。但是请注意,whitelionV的建议只是一种保存组成结构的位的方法。如果CMS_ContentInfo包含任何指针,它将不合适。@KevinBallard怎么做?我尝试过:NSMutableData*data;写入(encryptedDataWithCMS,sizeof(encryptedDataWithCMS),1,数据);而且。。。NSMutableData*temp=[NSMutableData dataWithBytes:encryptedDataWithCMS长度:sizeof(encryptedDataWithCMS)]@克丽丝:很明显,你不会用
write()
尝试在
NSMutableData
中放入一些东西。您可以使用
NSMutableData
上的方法,例如
-appendBytes:length:
代替
write()
调用。