main.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <stdlib.h> 4 5 using namespace std; 6 7 int main ( int argc, char* argv[] ) { 8 if ( argc != 4 ) { 9 cout << "Usage: ft <File> <Off> <Data>" << endl; 10 return -1; 11 } 12 fstream file ( argv[1], ios::in | ios::out | ios::binary | ios::ate ); 13 if ( file.is_open() ) { 14 long long fileSize = static_cast<long long> ( file.tellg() ); 15 cout << "Size: " << fileSize << " bytes" << endl; 16 long int yPos = strtol ( argv[2], NULL, 16 ); 17 long int yData = strtol ( argv[3], NULL, 16 ); 18 unsigned char sData = static_cast<unsigned char> ( yData ); 19 if ( yPos < 0 || yPos >= fileSize ) { 20 cout << "Overflow. [0 ~ " << fileSize - 1 << "]" << endl; 21 return -1; 22 } 23 std::ios_base::seekdir sPos = static_cast<std::ios_base::seekdir> ( yPos ); 24 file.seekg ( sPos ); 25 char cbit[8]; 26 itoa ( file.get(), cbit, 16 ); 27 cout << "Data: " << cbit << endl; 28 file.seekg ( sPos ); 29 file << sData; 30 file.close(); 31 cout << "Tampering successfully." << endl; 32 return 0; 33 } else { 34 cout << "Can not open file." << endl; 35 return -1; 36 } 37 }