简体   繁体   English

linux中的文件输入输出

[英]File input output in linux

I am trying to open a block file system in android in recovery mode which is currently mounted and after opening that file system i want to move the cursor location to the beginning of that block file.我试图在当前安装的恢复模式下在 android 中打开块文件系统,在打开该文件系统后,我想将光标位置移动到该块文件的开头。

int movecursor(const char* blockPartition)
{
    int fd_read = open(blockPartition, O_RDONLY);
    printf("fd_read=%d \n",fd_read); 

    // go to beginning
    int returncode1 = lseek(fd_read, 0, SEEK_SET);// Here it is returning -1
    printf("returncode1=%d and \n",returncode1);
}

Here i am able to open the file system in read mode that is currently mounted but when a am trying to move the cursor to beginning of that file it is returning -1 (operation not permitted).在这里,我能够以当前安装的读取模式打开文件系统,但是当我尝试将光标移动到该文件的开头时,它返回 -1(不允许操作)。

the above code snippet is running as a android native service.上面的代码片段作为 android 本机服务运行。

Please help me to move the cursor position at the beginning of a file system.请帮我移动文件系统开头的光标位置。

you can use access system call to check whether you have access rights or not before going to open the block partition.在打开块分区之前,您可以使用访问系统调用来检查您是否具有访问权限。

 if (access(partition, F_OK | W_OK) != -1) { printf("%s partition is available.Continuing...\\n", partition); /* Open the mtd device for reading and writing */ int fd_path = open(partition, O_RDONLY); lseek(fd_path, 0, SEEK_SET);// or lseek64 based on your system architecture }

In general, when "Bad File Descriptor" is encountered when your file descriptor which you passed into the function is not valid, which has multiple possible reasons:通常,当您传递给函数的文件描述符无效时遇到“Bad File Descriptor”时,可能有多种原因:

*The fd is already closed somewhere. * fd 已在某处关闭。 *The fd has a wrong value, which is inconsistent with the value obtained from open system call. * fd 有一个错误的值,与 open 系统调用得到的值不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM