简体   繁体   中英

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.

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).

the above code snippet is running as a android native service.

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:

*The fd is already closed somewhere. *The fd has a wrong value, which is inconsistent with the value obtained from open system call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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