Demonstrate gcount(). : File Read « File « C++






Demonstrate gcount().

Demonstrate gcount().
 


#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream out("test", ios::out | ios::binary);

  if(!out) {
    cout << "Cannot open output file.\n";
    return 1;
  }

  double nums[4] = {1.1, 2.2, 3.3, 4.4 };

  out.write((char *) nums, sizeof(nums));
  out.close();

  ifstream in("test", ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  in.read((char *) &nums, sizeof(nums));

  int i;
  for(i = 0; i <4; i++) 
    cout << nums[ i ] << ' ';

  cout << '\n';

  cout << in.gcount() << " characters read\n";
  in.close();

  return 0;
}

           
         
  








Related examples in the same category

1.Write char to a fileWrite char to a file
2.Read file contentRead file content
3.Use getline() to read a string that contains spaces.Use getline() to read a string that contains spaces.
4.Demonstrate peek() in ifstreamDemonstrate peek() in ifstream
5.Demonstrate seekg().
6.Read formatted data from a file.
7.Write unsigned char to a file and read it back