我正在开发一个压缩程序,它需要将文件作为单个数字读入 RAM 并执行基本的数学运算和位移。我看过 GNU 的 gmp ,但是它与 c/c++ 的集成太差了,我不知道从哪里开始读取并将值放入 mpz_t 变量中。
1 回答
1
#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;
}
编辑:修正,误解了原始问题,现在它将文件读取为数字而不是 ASCII 数字。
编辑:将整个文件移动到 mpz_class 转换为一个不错的函数。
于 2011-07-03T03:56:51.373 回答