Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 达到eof时的fstream读取行为_C++_C++11 - Fatal编程技术网

C++ 达到eof时的fstream读取行为

C++ 达到eof时的fstream读取行为,c++,c++11,C++,C++11,1.如果fstream::read()在读取长度字段的足够字符之前命中eof,会发生什么情况 2.在这种情况下,我如何知道已读了多少个字符 谢谢 while(in_file.read(buffer, buffer_length)){ //what happens if eof not at the end of size buffer_length temp = sendto(sender_socket, buffer, sizeof(buffer), 0, NULL,

1.如果fstream::read()在读取长度字段的足够字符之前命中eof,会发生什么情况

2.在这种情况下,我如何知道已读了多少个字符

谢谢

while(in_file.read(buffer, buffer_length)){
//what happens if eof not at the end of size buffer_length
            temp = sendto(sender_socket, buffer, sizeof(buffer), 0, NULL, 0); 
            byte_count += temp;
            msg_count ++;
}
我在这里试图做的是读取固定长度的文件并发送到sender_socket,但是对于最后一条消息,缓冲区很可能未填充,而不是sizeof(缓冲区)。如何确定我发送的大小。文件将采用二进制格式。

来自:

如果在成功读取
n
个字符之前,输入序列中没有要提取的字符(即到达文件末尾),则
s
指向的数组包含该点之前读取的所有字符,并且为流设置
eofbit
failbit
标志。

通过调用成员
gcount
,可以访问此函数成功读取和存储的字符数

因此,您可以:

while(in_file.read(buffer, buffer_length)){
    temp = sendto(sender_socket, buffer, sizeof(buffer), 0, NULL, 0); 
    byte_count += temp;
    msg_count ++;
}
streamcount n = in_file.gcount();
if (n > 0) { // read a partial buffer
    temp = sendto(sender_socket, buffer, n, 0, NULL, 0);
    byte_count += temp;
    msg_count++;
}

文档对此主题有何说明?为什么要贴标签?RTFM。如果您的文档没有涵盖这一点,您需要新的文档。谢谢,什么是“streamcount”?我可以在文件.gcount()中执行int n=吗?请阅读
gcount
的文档
streamcount
是它返回的整数类型的typedef。你提供的免费讲义越多,你重复的“阅读文档”邀请就越不可能被注意。