1

我正在我的代码中尝试以下内容:-

{

   int y,n_bytes;

   struct stat temp_data;

   y = fstat(netdev->queue_fd[class_id],&temp_data);
   printf("Success - %d , size -> %lld , Error- %d \n",y,temp_data.st_size,errno);
   n_bytes = write(netdev->queue_fd[class_id],buffer->data,buffer->size);
   y = fstat(netdev->queue_fd[class_id],&temp_data);
   printf("After write Success - %d , size -> %lld , Error- %d and the value of n_bytes is - %d ",y,temp_data.st_size,errno,n_bytes);

}

我得到的输出是:-

Success - 0, size -> 0 , Error - 11 
After write Success - 0, size -> 0, Error - 11 and the value of n_bytes is - 1526 

大小为0和错误号11的原因是什么?有没有其他方法可以获取文件的大小?

注意:这里Netdev->queue_fd[class_id]是一个文件描述符。在不同的调用中,n_bytes 的值在 {41,1514,66,..} 之间变化。(总是大于 0)

谢谢

4

2 回答 2

2
  1. 成功后的状态errno无关紧要。的值errno仅在失败时修改。 fstat()返回零,因此 errno 的值无关紧要。

  2. 返回什么write()write()您没有进行检查,因此您不知道调用后文件应该更大。

于 2015-04-29T10:34:16.780 回答
0

Netdev->queue_fd[class_id]是一个文件描述符

  • 常规文件或
  • 任何 sys/fs 条目或
  • 字符/块/网络设备文件?

如果它不是一个普通文件,那么它的大小将不会在写入命令后更新

检查文件类型S_ISREG()

于 2015-04-29T11:18:27.957 回答