read函数 java 返回值_使用read()函数时检查EOF

这是我第一次使用文件描述符进行阅读,现在我已经通过试验和错误测试了大约3个小时,而且我几乎让我的读者工作了!我只需要在命名管道上检查EOF时需要一些帮助 .

好的,所以我打开一个(好多个)命名管道,如下所示:

fds[j].fd = open(pipeNameo, O_RDWR) ; // storing it into my file descriptor array

然后我轮询命名管道以查看是否有任何事情发生(轮询在循环内):

int ret = poll(fds, numOfPipesUsed, timeout_msecs);

当某些事情发生时,我通过将写入的文件描述符发送到此函数来处理文件:

int processFileDes( int fd )

{

char buf[10] ;

read(fd, buf, 1) ;

char curr = buf[0] ;

while (curr != EOF)

{

if ( curr == ' ')

{

// do nothing it is a space

}

else if ( curr == '\n')

{

printf("NEW LINE!\n") ;

}

else

{

int num = curr - '0' ; // turns char number into an int

printf("Curr Num: %d\n", num) ;

}

printf("BEFORE\n"); // Gets stuck here when EOF, this is the string of output

read(fd, buf, 1) ;

printf("AFTER\n") ;

curr = buf[0] ;

}

printf("Success!\n") ; // this is never printed

return 0 ;

}

一切都很好,除了 the read() function gets stuck (waiting for a return I imagine) once all characters have already been read. 应该是EOF . 我需要能够检查EOF .

我尝试了一种解决方法,通过计算读取的字符数来阻止我的程序读取(因为我知道输入的大小),并且它会起作用,唯一的事情就是当最后一个字符后面有一个空格它导致我的循环poll返回1并在剩余的空间(无效输入)上再次运行 processFile() 以进行读取 .

请帮忙 :')

输入只是一个数字矩阵,如下所示:

0 1 1

2 0 3

1 0 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值