txt文件读取后,数据如何处理的问题
[code=C/C++][/code]
string filename;
cout << "Please enter the file name: ";
cin >> filename;
//cin.clear();
ifstream infile;
if(!open_file(infile,filename))
{
cerr << "No input file!" << endl;
return 1;
}
string line;
while(getline(infile, line))
{
cout<<line;
}
上述代码读取file.txt中的十六位数字:
0xEA6D, 0xEBF0, 0xED40, 0xEE5C, 0xEF62, 0xF04B, 0xF11C, 0xF1D9, 0xF284, 0xF31F, // 10
0xF3AD, 0xF430, 0xF4A8, 0xF517, 0xF57E, 0xF5DD, 0xF636, 0xF68A, 0xF6DA, 0xF723, // 20
0xF768, 0xF7A8, 0xF7E7, 0xF821, 0xF857, 0xF88A, 0xF8BD, 0xF8EC, 0xF918, 0xF942, // 30
用line一次读取一行,如何去除掉最后的"//10",然后再将十六进制转化成十进制存储,谢谢!
[解决办法]
#include "stdafx.h"#include <iostream>#include <fstream>#include <string>#include <vector>using namespace std;int main(int argc, char* argv[]){ string filename; cout << "Please enter the file name: "; cin >> filename; ifstream file(filename.c_str(), ios::in); string line; vector<int> vec; char seps[] = ", "; char* token = NULL; while(getline(file, line)) { char* buf = new char[line.length() + 1]; strcpy(buf, line.c_str()); char* p = buf; token = strtok(p, seps); while(NULL != token) { if(token == strstr(token, "0x")) { cout<<strtoul(token, NULL, 16)<<endl; vec.push_back(strtoul(token, NULL, 16)); } token = strtok(NULL, seps); } delete[] buf; buf = NULL; } return 0;}