iPhone6 +上的Xcode应用程序无法打开文件进行二进制读取

时间:2016-09-28 00:18:22

标签: ios objective-c

我试图打开文件' d.mp3'我添加到我的Xcode 8.0项目中 如果我双击Xcode>项目> ' d.mp3'文件播放得很好。
但是,当Xcode在我的iPhone6 +(iOS 10.0.1)上运行我的应用程序时,fopen()会为文件句柄返回0。

以下代码和输出......

bool read_next_chunk( char* relative_file_pathname, unsigned char* chunk_buffer, int chunk_size_bytes, FILE* file_handle  )

{     const bool dbg = true;

// Get absolute file pathname:
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *program_directory = [paths objectAtIndex:0];
    NSString* absolute_pathname = [NSString stringWithFormat: @"%@/%s", program_directory, relative_file_pathname ];

if( dbg )   printf("\n%s:  file '%s'.  Path '%s'.", __FUNCTION__, relative_file_pathname, [absolute_pathname UTF8String] );

// Open file if not already open:
if( file_handle == NULL )
{
    if( dbg ) printf("\n%s    Open file %s.", __FUNCTION__, relative_file_pathname );
    file_handle = fopen( [absolute_pathname UTF8String], "rb" );         // open for binary reading
    if( dbg ) printf("\n%s    file_handle %d.", __FUNCTION__, file_handle);
}
if( file_handle )
{
    // Read next chunk of file into file_content_string:
    int total_read_bytes = (int)fread( chunk_buffer, 1, chunk_size_bytes, file_handle );
    if( total_read_bytes != chunk_size_bytes )
    {
        printf("\n %s:   %d total_read_bytes != %d chunk_size_bytes -- FAULT!!  <<<<<<<<<<<",__FUNCTION__, total_read_bytes, chunk_size_bytes);
        return false;
    }
    return true;
}
else
{
    printf("\ %s:   Cannot open '%s' FAULT!!  <<<<<<<<<<<", __FUNCTION__, relative_file_pathname );
    return false;
}

}

read_next_chunk:file&#39; d.mp3&#39;。 Path&#39; /var/mobile/Containers/Data/Application/C8B87C49-C6CF-4677-B775-6B3DF6EFD908/Documents/d.mp3'。
read_next_chunk打开文件d.mp3。
read_next_chunk file_handle 0. read_next_chunk:无法打开&#39; d.mp3&#39;故障!! &LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;

1 个答案:

答案 0 :(得分:1)

正如rmaddy所说,它可能不在您的文档目录中。尝试这样的事情:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"d" ofType:@"mp3"];