Gerard-Meier

Reading a bunch of bytes into Packet.

Feb 19th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. int main(int argc, const char **argv ) {
  2.     //MasterNew master;
  3.     ifstream myfile;
  4.     myfile.open ("C:\\Users\\G. Meier\\Documents\\myfile.txt", ios::in|ios::binary|ios::ate);
  5.  
  6.     int len = 3;
  7.     char * memblock = new char [len + 1]; // allocate an extra byte for the null byte.
  8.     myfile.seekg (0, ios::beg);
  9.     myfile.read (memblock, len);
  10.  
  11.     // The magic fix :D
  12.     memblock[len] = '\0';
  13.  
  14.     // Out put before turning it into a packet:
  15.     cout << sizeof(memblock) << " " << memblock << endl;
  16.  
  17.     char * buffer = Packet::create(Packet::FILE, memblock);
  18.  
  19.     // Since the char * doesn't end with a null byte, tell the output stream to only show the first 8 bytes.
  20.     // If we don't do this it'll just randomly output some data.
  21.     cout.write(buffer, len + 5);
  22.  
  23.     return cin.get();
  24. }
Advertisement
Add Comment
Please, Sign In to add comment