I have the following code that gets the filepath from another module, opens the file and reads the content. The file is opening , but read fails with error: Bad file number. I then inserted a write command after open, which works, but read still fails.Can someone please tell me why read isnt working? Also , Im puzzled about why write() even works when im opening the file in R_ONLY mode.
          char* file_content = (char*) malloc (1024);
          int fd = open(filepath,"O_RDONLY");
          printf("I have attempted open file \n");
          fflush(stdout);
          bzero(file_content, 1024*sizeof(char));
          if(fd <= 0) { //open failed
            file_content = "Error opening file";
            printf("The error number is %d\n", errno);
            fflush(stdout);
          }
          else
          {
             if(write(fd, "hello", 5)<0){
                printf("write failed");
                fflush(stdout);
              }
            if(read(fd, file_content,1023) < 0){
                printf("Error! Read file as %d\n",errno);
                fflush(stdout);
            }
          }
Output is Error! Read file as 8. 8=Bad file number. Any help please?
 
    