0

I'm studying C++ and I don't know how to read blank word(like " ")

my code just reading only "words"

please help me

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char const *argv[]) {
  ofstream output("data4.dat", ios::out | ios::binary);
  string buffer = "A program that translates a high level languageto a machine language\n is called a compiler. A compiler is thus a somewhat peculiar sort\n of program and its output is yet another program. To avoid confusion,\n the input program is usually called the source program or\n source code, and the translated version produced by the compiler is called";
  output << buffer;
  output.close();

  ifstream is;
  ifstream input("data4.dat", ios::in | ios::binary);
  string in_buffer;



  if (! is.eof()) {
    for ( int i = 0; i < sizeof(input); i++) {
      input >> buffer[i];
    }
  }
  input.close();
  std::cout << buffer[2] << '\n';
  return 0;
}
james lee
  • 11
  • 3
  • The `operator< – super Mar 10 '18 at 03:02
  • thankyou! I understand that I have to use input.read, OK. – james lee Mar 10 '18 at 04:16
  • `sizeof(input)` will return the size of an `ifstream` object, the size of a bunch of bookkeeping information that represents a file. It does not return the number of bytes in a file. Fortunately you don't need to know how big the file is. You can keep reading bytes until the read fails. [But don't use `while(!input.eof())` because that trick doesn't work.](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Mar 10 '18 at 07:52

0 Answers0