ifstream.read()vs. ifstream.readsome()在MSVC ++ 7.1 [英] ifstream.read() vs. ifstream.readsome() in MSVC++7.1

查看:414
本文介绍了ifstream.read()vs. ifstream.readsome()在MSVC ++ 7.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是使用了一些在Linux下开发的文件读取器的旧代码,并试图在使用MSVC ++ 7.1编译的Windows项目中使用相同的代码。代码编译没有任何问题,但根据Windows上的文件读取器,该文件似乎是空的。

I just took some older code of a file reader that had been developed under Linux and tried to use that very same code in my Windows project compiled with MSVC++7.1. The code compiled without any problems, but the file seemed to be empty according to the file reader on Windows.

我跟踪到ifstream.readsome从文件中读取任何内容,而不在流上设置任何错误标志。下面提供的代码在Linux和Windows上编译,但是Linux工作正常。

I tracked the problem down to ifstream.readsome() that didn't read anything from the file, without setting any error flags on the stream. The code provided below compiles on either Linux and Windows, but Linux it works as expected.

代码打开一个文件,并使用 read()一次读取文件的前512个字节时间与 readsome()。两个结果存储在两个向量中,在程序结束时进行比较。预期的结果将是两个向量相等。

The code opens a file and reads the first 512 bytes of the file one time with read() and one time with readsome(). Both results are stored in two vectors that are compared at the end of the program. The expected result would be that the two vectors are equal.

Windows XP上的程序的结果输出在源代码下面。

The result output of the program on Windows XP is posted below the source code.

如果任何人有任何想法或猜测这段代码可能出错,我很乐意听到他们。

If anyone has any ideas or guesses what might go wrong in this code, I'd love to hear them.

#include <iostream>
#include <fstream>
#include <vector>
#include <cassert>

int main()
{
  const size_t BUFFER_SIZE(512);

  std::ifstream in("some.file", std::ios::in | std::ios::binary);
  if(in.is_open())
  {
    std::vector<char> buf1(BUFFER_SIZE);
    std::vector<char> buf2(BUFFER_SIZE);
    assert(buf1 == buf2);

    if(in.seekg(0, std::ios_base::beg).good())
    {
      //read BUFFER_SIZE bytes from the file using read()
      if(in.read(&buf1[0], buf1.size()).eof())
      {
        std::cerr << "read() only read " << in.gcount() << " bytes." << std::endl;
      }
      else
      {
        std::cout << "read() could read all 512 bytes as expected." << std::endl;
      }
      if(!in.good())
      {
        std::cerr << "read() set an error flag in ifstream." << std::endl;
      }
    }
    else
    {
  std::cerr << "Can not test read()." << std::endl;
    }
    if(in.seekg(0, std::ios_base::beg).good())
{
      //read BUFFER_SIZE bytes from the file using readsome()
      std::streamsize bytesRead = in.readsome(&buf2[0], buf2.size());
      if(bytesRead != BUFFER_SIZE)
      {
        std::cerr << "readsome() only read " << bytesRead << " bytes." << std::endl;
      }
      else
      {
        std::cout << "readsome() could read all 512 bytes as expected." << std::endl;
      }
      if(!in.good())
      {
        std::cerr << "readsome() set an error flag in ifstream." << std::endl;
      }
    }
    else
    {
      std::cerr << "Can not test readsome()." << std::endl;
    }

    //should read from the same file, so expect the same content
    assert(buf1 == buf2);
  }

  return 0;
}



Windows XP上的输出:


$ b b

Output on Windows XP:

read() could read all 512 bytes as expected.
readsome() only read 0 bytes.
Assertion failed: buf1 == buf2, file [...], line 60


推荐答案

readsome()是做非阻塞读取,因为正是这意味着实现定义(或由您的自定义streambuf成员 showmanyc()),行为可以有所不同。对我来说都是正确的。

readsome() is there to make non blocking reads, as what precisely that means is implementation defined (or defined by your custom streambuf member showmanyc()), behaviour can vary. Both seem correct to me.

这篇关于ifstream.read()vs. ifstream.readsome()在MSVC ++ 7.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆