目录

Accessing Application Files (C/C++)

When to Use

The FileIO module provides some basic file operation capabilities. For other capabilities, refer to the libc standard library and C++ standard library.

Constraints

Before performing file operations, ensure that the URI or path passed in is correct and valid.

Available APIs

For details about the APIs, see FileIO.

API Description
FileManagement_ErrCode OH_FileIO_GetFileLocation(char *uri, int uriLength, FileIO_FileLocation *location) Obtains the location of a file.
enum FileIO_FileLocation FileIO_FileLocation Enumerates the file locations.
enum FileManagement_ErrCode FileManagement_ErrCode Enumerates the error codes used in the FileIO module.

How to Develop

Adding the Dynamic Link Library

Add the following library to CMakeLists.txt.

target_link_libraries(sample PUBLIC libohfileio.so)

Adding the Header File

#include <cstdio>
#include <cstring>
#include <filemanagement/fileio/oh_fileio.h>

Call OH_FileIO_GetFileLocation to obtain the location of a file.

Example:

void GetFileLocationExample(char *uri)
{
    FileIO_FileLocation location;
    int uriLength = static_cast<int>(strlen(uri));
    FileManagement_ErrCode ret = OH_FileIO_GetFileLocation(uri, uriLength, &location);
    if (ret == 0) {
        if (location == FileIO_FileLocation::LOCAL) {
            printf("Succeeded in getting file location, this file is on local.");
        } else if (location == FileIO_FileLocation::CLOUD) {
            printf("Succeeded in getting file location, this file is on cloud.");
        } else if (location == FileIO_FileLocation::LOCAL_AND_CLOUD) {
            printf("Succeeded in getting file location, this file is on  local and cloud.");
        }
    } else {
        printf("Failed to get file location, error code is %d", ret);
    }
}