• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 应用文件访问(C/C++)
2<!--Kit: Core File Kit-->
3<!--Subsystem: FileManagement-->
4<!--Owner: @wangke25; @gsl_1234; @wuchengjun5-->
5<!--Designer: @gsl_1234; @wangke25-->
6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang-->
7<!--Adviser: @foryourself-->
8
9## 场景介绍
10
11FileIO模块提供了部分文件基础操作能力,其他能力请参考[libc标准库](../reference/native-lib/musl.md)/[标准C++库](../reference/native-lib/cpp.md)。
12
13## 约束限制
14
15进行文件操作之前,必须保证传入正确有效的URI或path。
16
17## 接口说明
18
19接口的详细说明,请参考[FileIO](../reference/apis-core-file-kit/capi-oh-fileio-h.md)。
20
21| 接口名称 | 描述 |
22| -------- | -------- |
23| FileManagement_ErrCode OH_FileIO_GetFileLocation(char *uri, int uriLength, FileIO_FileLocation *location)| 获取文件存储位置。|
24| enum FileIO_FileLocation FileIO_FileLocation| 文件存储位置枚举值。 |
25| enum FileManagement_ErrCode FileManagement_ErrCode| 文件管理模块错误码。|
26
27## 开发步骤
28
29**在CMake脚本中链接动态库**
30
31CMakeLists.txt中添加以下lib。
32
33```txt
34target_link_libraries(sample PUBLIC libohfileio.so)
35```
36
37**添加头文件**
38
39```c++
40#include <cstdio>
41#include <cstring>
42#include <filemanagement/fileio/oh_fileio.h>
43```
44
45调用OH_FileIO_GetFileLocation接口获取文件存储位置。示例代码如下所示:
46```c
47void GetFileLocationExample() {
48    char *uri = "file://com.example.demo/data/storage/el2/base/files/test.txt";
49    FileIO_FileLocation location;
50    FileManagement_ErrCode ret = OH_FileIO_GetFileLocation(uri, strlen(uri), &location);
51    if (ret == 0) {
52        if (location == FileIO_FileLocation::LOCAL) {
53            printf("This file is on local.");
54        } else if (location == FileIO_FileLocation::CLOUD) {
55            printf("This file is on cloud.");
56        } else if (location == FileIO_FileLocation::LOCAL_AND_CLOUD) {
57            printf("This file is both on local and cloud.");
58        }
59    } else {
60        printf("GetFileLocation failed, error code is %d", ret);
61    }
62}
63```
64