gpt4 book ai didi

android - 为 fopen 更改 Android 中文件的权限

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:21 31 4
gpt4 key购买 nike

我正在尝试使用 fopen 打开文件并在其中写入日志并将其保存回设备,但我似乎无法使用 fopenstrerror 返回 error 22:建议的位置是 /data。我在这里做了一些搜索,发现更好的位置是 /mnt/sdcard,比如 Download:

#include <stdio.h>
#include <android/log.h>
#include <errno.h>
#include <string.h>


int main()
{
//writing to a file
FILE * fp;
fp = fopen("logmsg.txt", "+wb");
if (fp != NULL) {
fprintf(fp,"File created successfully!\n");
printf("File created successfully!\n");
}
else {
printf("Failed to create the file.\n");
printf( "Error code opening file: %d\n", errno );
printf( "Error opening file: %s\n", strerror( errno ) );
return -1;
}
return 0;
}

但是,我似乎无法使用 chmod 更改权限,而且它没有返回任何错误。 /mnt 是否挂载了 noExec?任何解决方法表示赞赏。

一些信息:

Kernel: 3.18.31-g3d35637
CPU HW: Qualcomm Technologies, Inc APQ8096
Processor : AArch64 Processor rev 2 (aarch64)

更新-1:在@kiran 建议将 +w 更改为 w+ 之后,我将 error 30 作为 Read-only file system \data/mnt/sdcard/Download 上的 Permission Denied。我该如何克服这个问题?

更新 2:快速解决方法:我所做的是 adb shell mount -o rw 并且我能够保存我的文件

最佳答案

根据 errno.h错误号 22 是

#define EINVAL 22/* 无效参数 */

意味着您正在为 fopen 传递无效参数。

   fp = fopen("logmsg.txt", "+wb");

应该是

   fp = fopen("logmsg.txt", "wb+");

根据 fopen-man-page

参数模式指向以下列序列之一开头的字符串(可能后跟其他字符,如下所述):

r

Open text file for reading. The stream is positioned at the beginning of the file.

r+

Open for reading and writing. The stream is positioned at the beginning of the file.

w

Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

w+

Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

a

Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

a+

Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above.

关于android - 为 fopen 更改 Android 中文件的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52474453/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com