从管道读取:How to handle the return values in c?
read from a pipe: How to handle the return values in c?
我正在编写一个从管道读取的程序,我想知道处理 return 值的正确方法是什么。根据阅读手册页,
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.
我担心它可能只读取一半数据的情况。另外,当 return 值为零时,正确的处理方法是什么?
这是我的示例代码。
struct day
{
int date;
int month;
};
while(1)
{
ret = select(maxfd+1, &read_fd, NULL, &exc_fd,NULL);
if(ret < 0)
{
perror("select");
continue;
}
if(FD_ISSET(pipefd[0], &read_fd))
{
struct day new_data;
if((ret = read(pipefd[0], &new_data, sizeof(struct day)))!= sizeof(struct day))
{
if(ret < 0)
{
perror("read from pipe");
continue;
}
else if(ret == 0)
{
/*how to handle?*/
}
else
/* truncated read. How to handle?*/
}
}
...
}
我相信 read() 无法读取超过指定大小的数据。如有错误请指正
请帮助我处理读取的 return 值。
如果读取 returns 0,那么您的进程已经读取了来自该文件描述符的所有数据。将它从 read_fd 中取出,如果它是 maxfd,则将 maxfd 重置为最新的最大值。根据您的进程正在执行的操作,您可能还需要进行其他清理工作。如果你得到一个简短的阅读,那么要么处理你收到的数据,要么丢弃它,要么存储它,直到你获得所有数据并可以处理它。
很难对一个非常笼统的问题给出更具体的答案。
当您 read
请求给定数量的数据时,没有任何东西可以保证您可以读取与请求一样多的可用数据。例如,您可能遇到文件末尾,或者写入器部分没有在您的管道中写入太多数据。所以 read
returns 你有效读取了什么,也就是 读取的字节数被返回(零表示文件结束).
如果read
returns是一个严格的正数,就清楚了。
如果read
returns 0,则表示文件结束。对于常规文件,这意味着您当前位于文件末尾。对于管道,这意味着管道是空的 and,不会写入任何单个字节。对于管道,这意味着您已经读取了所有数据并且另一端不再有写入器(因此不会写入更多字节),因此您可以关闭现在无用的管道。
如果read
returns -1 这意味着发生了错误,你必须参考errno
变量来确定错误的原因麻烦了。
因此,一般架构可能类似于:
n = read(descriptor,buffer,size);
if (n==0) { // EOF
close(descriptor);
} else if (n==-1) { // error
switch(errno) { // consult documentations for possible errors
case EAGAIN: // blahblah
}
} else { // available data
// exploit data from buffer[0] to buffer[n-1] (included)
}
我正在编写一个从管道读取的程序,我想知道处理 return 值的正确方法是什么。根据阅读手册页,
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal.
我担心它可能只读取一半数据的情况。另外,当 return 值为零时,正确的处理方法是什么?
这是我的示例代码。
struct day
{
int date;
int month;
};
while(1)
{
ret = select(maxfd+1, &read_fd, NULL, &exc_fd,NULL);
if(ret < 0)
{
perror("select");
continue;
}
if(FD_ISSET(pipefd[0], &read_fd))
{
struct day new_data;
if((ret = read(pipefd[0], &new_data, sizeof(struct day)))!= sizeof(struct day))
{
if(ret < 0)
{
perror("read from pipe");
continue;
}
else if(ret == 0)
{
/*how to handle?*/
}
else
/* truncated read. How to handle?*/
}
}
...
}
我相信 read() 无法读取超过指定大小的数据。如有错误请指正
请帮助我处理读取的 return 值。
如果读取 returns 0,那么您的进程已经读取了来自该文件描述符的所有数据。将它从 read_fd 中取出,如果它是 maxfd,则将 maxfd 重置为最新的最大值。根据您的进程正在执行的操作,您可能还需要进行其他清理工作。如果你得到一个简短的阅读,那么要么处理你收到的数据,要么丢弃它,要么存储它,直到你获得所有数据并可以处理它。
很难对一个非常笼统的问题给出更具体的答案。
当您 read
请求给定数量的数据时,没有任何东西可以保证您可以读取与请求一样多的可用数据。例如,您可能遇到文件末尾,或者写入器部分没有在您的管道中写入太多数据。所以 read
returns 你有效读取了什么,也就是 读取的字节数被返回(零表示文件结束).
如果read
returns是一个严格的正数,就清楚了。
如果read
returns 0,则表示文件结束。对于常规文件,这意味着您当前位于文件末尾。对于管道,这意味着管道是空的 and,不会写入任何单个字节。对于管道,这意味着您已经读取了所有数据并且另一端不再有写入器(因此不会写入更多字节),因此您可以关闭现在无用的管道。
如果read
returns -1 这意味着发生了错误,你必须参考errno
变量来确定错误的原因麻烦了。
因此,一般架构可能类似于:
n = read(descriptor,buffer,size);
if (n==0) { // EOF
close(descriptor);
} else if (n==-1) { // error
switch(errno) { // consult documentations for possible errors
case EAGAIN: // blahblah
}
} else { // available data
// exploit data from buffer[0] to buffer[n-1] (included)
}