簡體   English   中英

使用本機ios API的iOS文件大小

[英]IOS filesize with native ios API

我正在使用Rad Studio 10.2 Tokyo,並且正在包裝IOS本機API。 我正在嘗試獲取一個名為/Documents/test.txt的文件的文件大小,該文件可以成功使用POSIX進行讀寫,但無論如何我都無法獲取文件大小。

我以不同的方式得到自己的道路:

我通過使用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代碼:

從本機代碼:

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

包裝到RadStudio中:

_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;

結果:fileSizeVal = 0

我嘗試使用統計結構

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

結果:size = 0

我注意到的唯一無法找到Apple Developer網站的信息是,AAAAAAAA-BB-CC-DD-ZZZZZZZZZZ每次都是不同的,但我成功讀寫文件后也發現了相同的“更改路徑”。

/ private / var / mobile /容器/數據/應用程序/AAAAAAAA-BBBB-CCCC-DDDD-ZZZZZZZZZZ/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中讀回它時,它會返回正確的字節。

最終,在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;

}

這是我獲取程序路徑的兩個例程:

    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的其中之一:

    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上運行簡單功能。 我希望有人會感謝我為社區所做的貢獻,並節省了人們的寶貴時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM