Saturday 7 April 2012

-54

 char sB = 0xCA;
 
  printf("sB: %X  %d\n", sB,sB);
  C = sB >> 1;
  printf("sB>>1: %X\n", C);



I forgot how come the sB equals -54?

Monday 2 April 2012

walkthrough 1

#include <fstream>
#include <iostream>
using namespace std;
int main(){
  int i,j;
  cout<<sizeof(i)<<endl;   // the output of this line is 4
  fstream f("f.bin",ios::out|ios::in|ios::binary|ios::trunc);
  for(i=10;i<100;i+=10){
    f.write((const char*)&i, sizeof(int));
  }
  f.seekg((ios::pos_type)20);
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark
  f.seekg(-(ios::off_type)sizeof(int), ios::end);
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark
  f.seekg(-(ios::off_type)sizeof(int)*3, ios::cur);
  f.read((char*)&i, sizeof(int));
  cout<<i<<endl; // 1 mark
  f.seekp((ios::off_type)sizeof(int)*2, ios::beg);
  f.write((char*)&i, sizeof(int));
  f.seekp((ios::off_type)0, ios::end);
  cout<<(j=f.tellp())<<endl;  // 1 mark
  f.seekg(0);
  while(f.tellg() < j){  // 1 mark
    f.read((char*)&i, sizeof(int));
    cout<<i<<", ";
  }
  cout<<endl;   
  return 0;
}



output:  4
            60
            90
            70
            36
            10 ~ 90

fstream

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

int main(){
  fstream file("datafile.bin",ios::in|ios::binary);
  int i;
  while(file.good()){
    file.read((char*)&i, sizeof(i));
    if(file.good())
      cout<<i<<", ";
  }
  file.close();// no need
  return 0;
}




Why the output is end of 9999,begin with 6012?