-2
텍스트 파일을 읽고 변수 (CFStringRef)에 복사하는 가장 쉬운 방법은 무엇입니까?Apple에서 Core Foundation을 사용하여 텍스트 파일을 읽으시겠습니까?
텍스트 파일을 읽고 변수 (CFStringRef)에 복사하는 가장 쉬운 방법은 무엇입니까?Apple에서 Core Foundation을 사용하여 텍스트 파일을 읽으시겠습니까?
단순히 CFStringRef 변수를 사용하고 Foundation 사용에 신경 쓰지 않는다면 가장 쉬운 방법은 파일 시스템에서 읽어와 ARC에서 캐스팅하는 NSString의 이니셜 라이저 중 하나를 사용하는 것입니다.
이 파일의 바이트 버퍼를 얻기 위해 표준 C 라이브러리 함수를 사용할 필요가이 경우FILE * file;
size_t filesize;
unsigned char * buffer;
// Open the file
file = fopen("/path/to/file", "r");
// Seek to the end to find the length
fseek(file, 0, SEEK_END);
filesize = ftell(file);
// Allocate sufficient memory to hold the file
buffer = calloc(filesize, sizeof(char));
// Seek back to beggining of the file and read into the buffer
fseek(file, 0, SEEK_SET);
fread(buffer, sizeof(char), filesize, file);
// Close the file
fclose(file);
// Initialize your CFString
CFStringRef string = CFStringCreateWithBytes(kCFAllocatorDefault, buffer, filesize, kCFStringEncodingUTF8, YES);
// Release the buffer memory
free(buffer);
: 물론
NSString * string = [NSString stringWithContentsOfFile:@"/path/to/file" encoding:NSUTF8StringEncoding error:nil];
CFStringRef cfstring = CFBridgingRetain(string);
당신은 순수 CF 솔루션은 나는 이런 식으로 뭔가를 제안하려는 경우 내용. 메모리 버퍼에로드하기에 너무 큰 파일을 다루는 경우 mmap 함수를 사용하여 파일을 메모리 맵핑하는 것이 쉽습니다. NSData는 대부분의 경우이 작업을 수행합니다.
C 입출력 라이브러리를 사용하여 종료되었습니다. CF 라이브러리 만 사용하는 솔루션이 있는지 궁금합니다. –