我正在尝试使用以下代码在文件头中写入一些数据(数据长度为 367 字节):
const char *attrName = [kAttributeForKey UTF8String];
const char *path = [filePath fileSystemRepresentation];
const uint8_t *myDataBytes = (const uint8_t*)[myData bytes];
int result = setxattr(path, attrName, myDataBytes, sizeof(myDataBytes), 0, 0);
当我尝试阅读它时,结果有所不同:
const char *attrName = [kAttributeForKey UTF8String];
const char *path = [filePath fileSystemRepresentation];
int bufferLength = getxattr(path, attrName, NULL, 0, 0, 0);
char *buffer = malloc(bufferLength);
getxattr(path, attrName, buffer, bufferLength, 0, 0);
NSData *myData = [[NSData alloc] initWithBytes:buffer length:bufferLength];
free(buffer);
有人可以告诉我如何使这项工作吗?提前致谢。