質問

私はfopen("/test.txt","w+")のように直接ファイルを開くしようとしました。それはシミュレータ上で動作しますが、iPhone上では動作しません。

それから私はこれを試してみた:

NSString *path = [[NSBundle mainBundle] pathForResource: @"GirlName.txt" ofType:nil];
NSLog(path);
fichier = fopen([path cStringUsingEncoding:1],"w+");

if (fichier != NULL)
{
    ...
}
else
{
    perror("error ");
}

私は、コンソールの許可で受信拒否ます:

2009-07-24 17:17:27.415 Mademoiselle[897:20b]
/var/mobile/Applications/.../....app/GirlName.txt
error : Permission denied

あなたは間違っているものを私に教えてください。

役に立ちましたか?

解決

あなたはアプリケーションバンドル内のファイルを開いて、作ることができないこと、書き込み可能(「W +」)。

あなたはモードを変更する場合は、

この同じコードは、おそらく(私はそれをテストしていない)に動作しますが「R」

あなたが書き込み可能なテキストファイルが必要な場合は、

、あなたは私がこのような何かをしたい、書き込み可能なディレクトリにで配置する必要があります:

//Method writes a string to a text file
-(void) writeToTextFile{
    //get the documents directory:
    NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //make a file name to write the data to using the documents directory:
    NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt", 
                         documentsDirectory];
    NSString *content = @"Test Copy Here";
    //save content to the documents directory
    [content writeToFile:fileName 
              atomically:NO
                encoding:NSStringEncodingConversionAllowLossy 
                   error:nil];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top