mardi 2 juin 2020

Unavailable to read integer value from a file with std::ifstream in FormattedInputFunction mode

As titled. It would work if I change the type of variable v to char. How can I read integer value directly with ">>" formatted input function instead of std::basic_istream::read from a binary file?

int main(int argc, char** argv) {
  //char v;  // This line will work fine.
  int v;
  std::ifstream in("./test", std::ios::binary);
  std::istream::sentry sentry(in);
  in >> std::noskipws;
  if (sentry) {
    while (true) {
      in >> v;
      if (in.fail()) {
        std::cout << "Reading Failed!" << std::endl;
        return 1;
      }
      std::cout << v << std::endl;
    }
  }
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire