0

我怎样才能做到这一点?我正在使用 Visual Studio 2010 C++。

char * Buffer = new char[Filesize];
//Fill it with data here

std::ifstream BinaryParse(Buffer, std::ios::binary);
if(BinaryParse.is_open())
{
   BinaryParse.read((char*)&Count, sizeof(unsigned int));
}

那是行不通的。除了从 char 数组读取之外,如何使 ifstream 的行为方式与读取文件相同?

4

1 回答 1

1

You can try istringstream, which uses a C++ string as the input stream.

Here is an example from C++ reference:

// using istringstream constructors.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main () {
  int n,val;
  string stringvalues;

  stringvalues = "125 320 512 750 333";
  istringstream iss (stringvalues,istringstream::in);

  for (n=0; n<5; n++) {
    iss >> val;
    cout << val*2 << endl;
  }

  return 0;
}

You can find another example here: http://www.fredosaurus.com/notes-cpp/strings/stringstream-example.html.

于 2012-05-25T00:02:05.160 回答