使用 fstream 读取文件然后关闭并再次读取在 Android 上不起作用

Reading file with fstream then closing and reading again wont work on Android

我遇到一个问题,如果我写入一个文件并从中读取,那么一切都很好。但是如果我关闭它并重新打开它,所有数据都消失了。

它在 Android 上 运行 并且 filePath2 来自 getExternalFilesDir 所以我不需要写入磁盘的权限。这是一个最小的可重现示例,它将 运行 在 Android Studio 上的新 JNI 项目上:

    ALOGV(TAG, "gonna open filePath2: %s", filePath2.c_str());
    f.open(filePath2, std::ios_base::binary | std::ios_base::trunc | std::ios_base::in | std::ios_base::out);
    if (f.is_open()) {
        ALOGV(TAG, "is open");
    } else {
        ALOGV(TAG, "is closed");
    }
    //Write some data to vector
    std::vector<char> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    //Write the vector to file
    f.write(v.data(), v.size());
    f.flush();
    //Lets read so we see that things were written to the file
    ALOGV(TAG, "seeking g and p to beggining");
    f.seekg(0, std::ios::beg);
    f.seekp(0, std::ios::beg);
    auto v2 = std::vector<char>(v.size());
    //Read things back to file
    f.read(v2.data(), v2.size());
    if (f) {
        ALOGV(TAG, "did read everything");
    } else {
        ALOGV(TAG, "did read only : %d", f.gcount());
    }
    //prints contents of the file
    auto* b1 = v2.data();
    for (int i=0; i<v2.size(); i++) {
        ALOGV(TAG, "b1[%d] = %d", i, b1[i]);
    }
    //finally closes the file
    f.close();
    //Lets open again to see if modifications were recorded permanently
    ALOGV(TAG, "gonna open filePath2: %s", filePath2.c_str());
    f.open(filePath2, std::ios_base::binary | std::ios_base::trunc | std::ios_base::in | std::ios_base::out);
    if (f.is_open()) {
        ALOGV(TAG, "is open");
    } else {
        ALOGV(TAG, "is closed");
    }
    ALOGV(TAG, "seeking g and p to beggining");
    f.clear();
    f.seekg(0, std::ios::beg);
    f.seekp(0, std::ios::beg);
    std::vector<char> v3(v.size());
    f.read(v3.data(), v3.size());
    if (f) {
        ALOGV(TAG, "did read everything");
    } else {
        ALOGV(TAG, "did read only : %d", f.gcount());
    }
    //lets print the contents again:
    auto* b3 = v3.data();
    for (int i=0; i<v3.size(); i++) {
        ALOGV(TAG, "b3[%d] = %d", i, b3[i]);
    }
    f.close();

这是输出:

V/MultiChannelRec.h(32416): gonna open filePath2: /storage/emulated/0/Android/data/com.my_name.flutter_app/files/something_1.txt
V/MultiChannelRec.h(32416): is open
V/MultiChannelRec.h(32416): seeking g and p to beggining
V/MultiChannelRec.h(32416): did read everything
V/MultiChannelRec.h(32416): b1[0] = 1
V/MultiChannelRec.h(32416): b1[1] = 2
V/MultiChannelRec.h(32416): b1[2] = 3
V/MultiChannelRec.h(32416): b1[3] = 4
V/MultiChannelRec.h(32416): b1[4] = 5
V/MultiChannelRec.h(32416): gonna open filePath2: /storage/emulated/0/Android/data/com.my_name.flutter_app/files/something_1.txt
V/MultiChannelRec.h(32416): is open
V/MultiChannelRec.h(32416): seeking g and p to beggining
V/MultiChannelRec.h(32416): did read only : 0
V/MultiChannelRec.h(32416): b3[0] = 0
V/MultiChannelRec.h(32416): b3[1] = 0
V/MultiChannelRec.h(32416): b3[2] = 0
V/MultiChannelRec.h(32416): b3[3] = 0
V/MultiChannelRec.h(32416): b3[4] = 0

如您所见,在关闭文件之前,我可以读取我写的内容。但是在我关闭并再次打开它之后,我读到了零。

通过在 Android 上使用文件管理器,当我转到应用程序的文件夹时,我看到文件已创建但有 0 个字节。

重新打开文件时删除 std::ios_base::trunc 标志:

discard the contents of the stream when opening

写入文件后,将其关闭并重新打开,您将清除之前写入的所有内容。