How do I read X file into RAM as a number in C++ in a linux environment? -
i'm working on compression program needs read file ram single number , perform basic math operations , bit shifting. i've looked @ gmp gnu, has such poor integration c/c++ have no idea begin read , put values mpz_t variable.
#include <fstream> #include <gmp.h> #include <gmpxx.h> #include <iostream> using namespace std; mpz_class filetonumber (const string& filename) { mpz_class number; ifstream file(filename.c_str()); while( file.good() ){ unsigned char c; file >> c; number <<= 8; number += c; } file.close(); return number; } int main (int argc, char* argv[]) { if( argc - 1 < 1 ){ cout << "usage: " << argv[0] << " file.txt" << endl; return 0; } cout << hex << filetonumber(argv[1]) << endl; } edit: fixed, misunderstood original question, reads files number instead of ascii number.
edit: moved entire file mpz_class conversion nice function.
Comments
Post a Comment