我有以下代码从另一个模块获取文件路径,打开文件并读取内容。文件正在打开,但读取失败并出现错误:文件编号错误。然后我在打开后插入了一个写入命令,该命令有效,但读取仍然失败。有人可以告诉我为什么读取不起作用吗?此外,我对为什么 write() 甚至在我以 R_ONLY 模式打开文件时有效。
          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);
            }
          }
输出错误!读取文件为 8。8=错误的文件编号。请问有什么帮助吗?