2017-08-24 4 views
1

私はRad Studio 10.2 Tokyoを使用しており、IOS Native APIをラッピングしています。 私は/Documents/test.txtというファイルのファイルサイズを取得しようとしていますが、これはPOSIXを使って書き込みと読み取りができますが、とにかくファイルサイズを取得できません。ネイティブIOS APIを使用したIOSファイルサイズ

私はさまざまな方法で私のパスを取得:

私はPOSIX APIを使用してさまざまな方法を試してみました:

char path [256]; 
memset (path, 0, sizeof(path)); 
strcpy(path, getenv("HOME")); 
strcat(path, "/Documents/test.txt"); 
long long fileSizeVal; 

// Using lseek 
int filesize= 0; 
int fd = -1; 
char path [256]; 

memset (path, 0, sizeof(path)); 

//HOME is the home directory of your application points to the root of your sandbox 
strcpy(path, getenv("HOME")); 

//concatenating the path string returned from HOME 
strcat(path, "/Documents/test.txt"); 

fd = open (path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); 

if (fd == -1) { 
    return -1; 
} 

filesize = lseek(fd, 0, 2); 

結果:ファイルサイズ= 0

を私はObjective-をラップすることにより、試してみましたCコード:

ネイティブコードから:

RadStudioにラップ
NSDictionary* attributeDict = [[NSFileManager defaultManager] attributesOfItemAtPath:resourcePath error:nil]; 
NSNumber* fileSizeObj = [attributeDict objectForKey:NSFileSize]; 
long long fileSizeVal = [fileSizeObj longLongValue]; 

_di_NSFileManager FileManager = TNSFileManager::Wrap(TNSFileManager::OCClass->defaultManager()); 
_di_NSDictionary attributeDict = FileManager->attributesOfFileSystemForPath(NSSTR(path), NULL); 
_di_NSNumber fileSizeObj = TNSNumber::Wrap(TNSNumber::OCClass->numberWithLongLong(attributeDict->fileSize())); 

fileSizeVal = fileSizeObj->longLongValue(); 
return fileSizeVal; 

結果:サイズ= 0

:fileSizeVal = 0

I結果stat構造体を

struct stat stat1; 
if(stat(path, &stat1)) { 
    // something is wrong 
} 
long long size; 
size = stat1.st_size; 
return size; 

を使用してみました

私がApple Developerサイトのリファレンスを見つけることができなかった唯一の事は、AAAAAAAA-BB-CC-DD-ZZZZZZZZは毎回違うが、 。

/private/var/mobile/Containers/Data/Application/AAAAAAAA-BBBB-CCCC-DDDD-ZZZZZZZZ/Documents/test.txt

私が期待するべきである一方で、何かのように:

/Users/steve/Documents/MyFile.txt

私はタフなこの仮定で間違っている可能性があります。

私は非常に不満です。

ファイルがそれを書いた後、存在する場合、私がチェック:

bool Exists = false; 
_di_NSFileManager FileManager = TNSFileManager::Wrap(TNSFileManager::OCClass->defaultManager()); 
if (FileManager) 
{ 
    Exists = FileManager->fileExistsAtPath(NSSTR(path)); 
    if (Exists == true) 
     return 1; 
    else 
     return 0; 
} 

をそして、それは正しくファイルが正しく書かれているように、答えを返し、私はPOSIXに戻ってそれを読んだとき、それは私に正しいバイトを返します。

+0

あなたは読んで/書くことができますか?ファイル(あなたは実用的なURLを持っていることを意味します)? – Dopapp

+1

iOSアプリです。なぜパスが "/Users/steve/Documents/MyFile.txt"のようなものになると思いますか?これはMacのパスであり、iOSデバイスのパスではありません。 – rmaddy

+0

あなたのファイルがどこにあるのか推測しないでください。ファイルが存在するかどうか確認しましたか? – clemens

答えて

0

最後に、XCodeでコードをコンパイルした後、RadStudioでコンパイルされたコードとX-Codeの動作が違うことに気付きました。

しかし、用事、私は、iOS上でRadStudioでPOSIXを使用してファイルを書く、読むために私の解決策を投稿することを嬉しく思います:

int iOS_WriteFile (char * FileName, char * BufferToWrite, int Lenght, int mode) 
{ 
    int fd = -1; 
    int iResult = -1; 
    char path [PATHSIZE]; 
    bool FileExists = false; 
    //mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 

    memset (path, 0, sizeof(path)); 

    iOS_GetApplicationPath (path); 
    strcat (path, FileName); 

    // Check if a NEWFILE is needed or append to existing one 
    if (mode == NEWFILE) 
    { 
    // Creates it (if it does not already exist), or truncates its length to 0 (if it does exist). 
    fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 
    // This creates a file if it does not exist 
    //fd = open(path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 
    } 
    if (mode == APPEND) 
    { 
     FileExists = iOS_FileExists (FileName); 
     if (FileExists == false) { 
      return IOS_ERROR_FILE_DOES_NOT_EXIST; // File Does not exist return appropriate ERROR 
     } 
     // File Exists so write it in append mode 
     fd = open(path, O_WRONLY | O_APPEND); 
    } 
    if (fd < 0) { 
     return IOS_ERROR_FILE_OPEN_ERROR; 
    } 

    //Writing Buffer 
    iResult = write (fd, BufferToWrite, Lenght); 
    if (iResult != Lenght) { 
     // Something went Wrong in writing the file 
     //Closing the file 
     close(fd); 
     return IOS_ERROR_FILE_WRITE_LENGH_ERROR; 
    } 

    //Closing the file 
    close(fd); 
    return iResult; 

} 

ここではアプリケーションのパスを取得するには、私の2つのルーチンが来る:

void iOS_GetApplicationPath (char * ApplicationPath) 
    { 
     char path [PATHSIZE]; 
     memset (path, 0, sizeof(path)); 
     //---- Get Path with "getenv" ------- 
     //HOME is the home directory of your application points to the root of your sandbox 
     strcpy(path, getenv("HOME")); 
     strcpy (ApplicationPath, path); 
    } 
包まれたiOSのAPIを使用して、この1の

:ここ

void iOS_Native_GetApplicationPath (char * ApplicationPath) 
    { 
     char path [PATHSIZE]; 
     // Get Home Directory using NSHomeDirectory() function 
     void * GetHomeDir = NSHomeDirectory(); 
     char * cHomeDir; 
     // Get Home Directory 
     _di_NSString HomeDir = TNSString::Wrap(NSHomeDirectory()); 
     UnicodeString HD; 
     // Convert Home Directori NSString to UnicodeStrting 
     HD = NSStrToStr (HomeDir); 
     // Stitch filename to Home Directory 
     memset (path, 0, sizeof(path)); 
     Mobile_ConvertUnicodeStringToChar (HD, path); // Convert UnicodeString to char 
     strcpy (ApplicationPath, path); 

    } 

は私のリードルーチンです:

int iOS_ReadFile (char * FileName, char * BufferToReturn, int Length) 
{ 
     int fd = -1; 
     int iResult = -1; 
     char path [PATHSIZE]; 
     bool FileExists = false; 
     char * ReadBuffer; 

     memset (path, 0, sizeof(path)); 

     iOS_GetApplicationPath (path); 
     strcat (path, FileName); 

     // Open in Read Only 
     fd = open(path, O_RDONLY); 
     if (fd > 0) { 
      // Read File 
      ReadBuffer = (char*) malloc(sizeof(char) * (Length + 1)); 
      iResult = read(fd, ReadBuffer, Length); 
      if (iResult != Length) { 
       close (fd); 
       return IOS_ERROR_FILE_READ_LENGH_ERROR; 
      } 
      else 
      { 
       memcpy (BufferToReturn, ReadBuffer, Length); 
       close (fd); 
       return iResult; 
      } 
     } 
     // File does not open 
     if (fd < 0) { 
      return IOS_ERROR_FILE_OPEN_ERROR; 
     } 
    } 

とそれがすべてそれがすべてのルーチンのファイルサイズで終わる始めたところ、最終的:

long long iOS_GetFileSize (char * filename) 
    { 
     char path [PATHSIZE]; 
     memset (path, 0, sizeof(path)); 
     iOS_GetApplicationPath (path); 
     strcat (path, filename); 

     int fd = -1; 
     long long FileSize; 
     // Open in Read Only 
     fd = open(path, O_RDONLY); 
     if (fd > 0) { 
      // Get untill the end of the file 
      FileSize = lseek(fd, 0, SEEK_END); 
      close (fd); 
     } 
     if (fd < 0) { 
      return IOS_ERROR_FILE_OPEN_ERROR; 
     } 
     else 
      return FileSize; 

    } 

ここの人々は非常にお互いを助けていない理由を私は知りません。これは、EmbarcaderoのRad StudioでPosix APIを使用して、iOSで簡単な機能を動作させるために一週間の苦労を重ねた結果です。私は誰かが私のコミュニティへの貢献に感謝し、人々の貴重な時間を節約してくれることを願っています...

+0

ここにいる人は、お互いをよく助けます。しかし、私たちはすべてのPOSIXの専門家ではありません。私ができるなら、私はあなたを助けてくれたでしょう。あなたがそれを理解してうれしいです。 – Mozahler

関連する問題