使用write()函数时出现错误的文件描述符

时间:2013-03-02 15:25:22

标签: c file file-descriptor

我想编写一个同时传输更多文件的程序。在某一点上,我收到错误的文件描述符错误。 我做了什么? 我的所有文件按其大小递减顺序(我按此顺序打开它们,f-> filename [i]不会更改)。     我修改了这些文件的权限:chmod ugo + wrx。     完全发送文件后,我在“for”之后减少numberOfFiles。     我的问题是,当我想写入一个文件时,它返回错误的文件描述符,但是当我想关闭该文件时它工作正常。

以下是不起作用的代码:

//open all the files that I want to send
        f->fileIndex[i] = open(f->fileName[i],O_RDONLY | O_CREAT);
        if (f->fileIndex[i] < 0 ) {
          perror("ERROR: Cannot open/create the file \n");
        }
....
for (i=1; i<numberOfFiles; ++i){
          if (write(f->fileIndex[i],aux,sz) == -1) {
            perror("ERROR Cannot write\n");
          }
          printf("%d %s\n",f->fileIndex[i],aux);
// aux = what I want to write
...
          if (f->size[i] <= 0) { //If the whole file was sent
            printf("close the file  %s\n",f->fileName[i]);
            if (close(f->fileIndex[i])) { // and here it works fine ...
              perror("cannot open the file\n");
            }
          }
}

我检查过,它与file_descriptor的值相同。 你能问我错在哪里吗? 任何提示都会有所帮助! 谢谢!

0 个答案:

没有答案
相关问题