Error using fopen() on a file in objective C, created in Windows

那年仲夏 提交于 2019-12-13 03:49:57

问题


This is related to my question here: problem opening file in ios container using fopen()

I have a file in iphone's container at

/var/mobile/Containers/Data/Application/.../Documents/file.wav  

I have changed ViewController.m to ViewController.mm

This is the code under test

#import "ViewController.h"

#include <stdio.h>

NSArray *documentPaths;
NSString *documentsDir;
NSString *inputWavFile;
NSString *outputWavData;
const char *documentsDirCver;
const char *outputWavDataCver;

@interface ViewController ()

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDir = [documentPaths objectAtIndex:0];
    documentsDir     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingString:@"/"]];
    // inputWavFile     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingString:@"file1.WAV"]]; 
    // outputWavData     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingString:@"file_op_1.TXT"]];     
    // documentsDir     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingPathComponent:@"/"]];
    inputWavFile     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingPathComponent:@"file1.WAV"]];     
    // inputWavFile     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingPathComponent:@"file_old.WAV"]];

    outputWavData     = [[NSString alloc] initWithFormat:@"%@",[documentsDir stringByAppendingPathComponent:@"file_op_1.TXT"]]; 


    documentsDirCver = [ inputWavFile UTF8String ];      // cannot be shared between files; documentsDirCver will point to the UTF8String version of the inputWavFile NSString, which does not have scope outside this file
    outputWavDataCver = [ outputWavData UTF8String ];

    // documentsDirCver = [ [NSFileManager defaultManager] fileSystemRepresentationWithPath:inputWavFile ];
    // outputWavDataCver = [ [NSFileManager defaultManager] fileSystemRepresentationWithPath:outputWavData ];

    // strcpy(documentsDirCver, [ inputWavFile UTF8String ]);
    // strcpy(outputWavDataCver, [ outputWavData UTF8String ]);

    // strcpy(documentsDirCver, [ [NSFileManager defaultManager] fileSystemRepresentationWithPath:inputWavFile ]);
    // strcpy(outputWavDataCver, [ [NSFileManager defaultManager] fileSystemRepresentationWithPath:outputWavData ]);

    // /var/mobile/Containers/Data/Application/8870579F-9B27-42FC-80B8-0FE54E695871/Documents/
    // /var/mobile/Containers/Data/Application/0246E350-47F8-4759-8CC7-1F12F322321D/Documents/
    printf("path: %s\n", documentsDirCver);
    printf("path: %s\n", outputWavDataCver);

// /*
    FILE *fptr;
    // if(fopen(documentsDirCver, "rb") == NULL)
    fptr = fopen(documentsDirCver, "rb");
    // if(fopen(documentsDirCver, "rb+") == NULL)
    if(fptr == NULL)
    {
        printf("Error opening file\n" );
        printf("errorno = %d\n", errno);
    }
    else{

    }
 // */


@end  

This is part of a much bigger project but this is the only code relevant for now

The printf() functions show the correct path, and other things I have tried are in the comments, but they didn't work either, and give me the same error

I am getting errno = 2 (file not found) when I use file1.wav as the input file. file1.wav exists, however, inside Documents in the ios container for this app. I have also a similar file handling operation before, and if I use this file (file_old.wav) it is detecby fopen() normally.

I have been going at it for hours but can't find any solution. Only thing I can think of now is some kind of file permission error, as file1.WAV was made in Windows recently, while I am not sure where file_old.wav had come from. I tried making a new wav file in audacity (file2.wav) but it gives me the same error.

来源:https://stackoverflow.com/questions/57864235/error-using-fopen-on-a-file-in-objective-c-created-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!