2012-04-10 10 views
5

C++ sınıfları ve FFmpeg kullanan bir projem var, fopen kullanmalı ve uygulama sanal alanına bir dosya yazmalıyım. C yazma ++ eşdeğerdir:iOS projesi için bir C++ sınıfında fopen kullanarak bir dosyaya yazmanız gerekiyor

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docs_dir = [paths objectAtIndex:0]; 

Bu bana ben çok fazla soru ben kullanabilmesi C++ bu kodu yazılacaktır? nasıl benim dosyalarım işleyebilirsiniz benim app kum havuzu, hiç olur Bir dosyada fopen mi var?

Bu

ihtiyacı yöntem uygulaması:

int testGrabAndWrite(const char* streamURL, bool decode, const char* filename) 
{ 
    FILE *outfile; 
    int ret; 
    int counter = 0; 
    uint8_t *data;    // Pointer to the received audio mem 
    int size;     // Size of the received audio buffer 


    outfile = fopen(filename, "w"); 

    if (outfile == NULL) 
     exit(1); 

    // Open the RTP stream 
    if ((ret = openStream(streamURL, decode)) < 0) 
     return ret; 

    // Print out info about the stream found 
    int tmpSampleRate, tmpBitRate, tmpChannels; 
    ret = getStreamInfo(tmpSampleRate, tmpBitRate, tmpChannels); 
    printf("\nSample rate:%d Bit rateL%d Channels:%d\n",tmpSampleRate,tmpBitRate, tmpChannels); 

    // Grab some sample data and write it to file. 
    while (counter < 500) 
    { 
     ret = getStreamData(data, size); 
     fwrite(data, 1, size, outfile);        // Write RTP packets, i.e. mp3, to file. 
     printf("Wrote packet %d with size %d which returned %d. ", ++counter, size, ret); 
    } 

    fclose(outfile); 
    closeStream(); 

    return ret; 
} 
+2

'a dönüştürülmüştür. fopen()', C++ değil, "fstream" sınıfını ve arkadaşlarını kullanan bir C API'sidir. Entegre etmeniz gereken dersleri bize göster. – trojanfoe

+1

bir .mm dosyası oluşturun .. C++ ve object-c –

cevap

7
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docs_dir = [paths objectAtIndex:0]; 
NSString* aFile = [docs_dir stringByAppendingPathComponent: @"somedocthatdefinitelyexists.doc"]; 

FILE* fp = fopen([aFile fileSystemRepresentation], "r"); 

bir char * uygun olarak taneciklere örneğin bir dosya sistemi kodlanan retruns -fileSystemRepresentation mesajı Muhtemelen UTF-8

+0

her ikisini de kullanabilirsiniz. Bu harika çalıştı, teşekkürler –

+0

Bu, simülatör için çalışıyor ancak 4GS'de null olarak döndürdüğünü fark ettim. Bunu fark ettiğinden emin değilsin. Anladığımda denemeyi dene. – Dev2rights

+0

@ Dev2Rights: Dosya eksik veya okunamıyorsa yukarıdaki kodun gerçek bir cihazda çalışmamasının bir nedeni yoktur. Errno'nun değeri nedir – JeremyP

İlgili konular