我有以下for循环:
ofstream myfile;
myfile.open ("proc.txt", ios::app);
for (unsigned int i=0; i<res.size(); ++i)
{
std::cout << res[i] << std::endl;
myfile << res[i] << "\n";
}
myfile.close();
如何将所有数据保存res[i]
到数组或缓冲区中,然后在循环结束后整理日期,使其唯一,然后打印出来并保存到文件中(这样我就没有重复的数据)?
在我的示例中,我的代码输出显示了一些数字,例如:
123456
123456
12345678
但我只需要:
123456
12345678
我的完整代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <algorithm>
#include <iterator>
#include <regex>
void find_locs(HANDLE process) {
unsigned char *p = NULL;
MEMORY_BASIC_INFORMATION info;
for ( p = NULL;
VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
p += info.RegionSize )
{
std::string buffer;
if (info.State == MEM_COMMIT &&
(info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE))
{
DWORD bytes_read;
buffer.resize(info.RegionSize);
ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
buffer.resize(bytes_read);
const std::tr1::regex rx("(^[0-9]{8,10}$");
std::tr1::match_results<std::string::const_iterator> res;
std::tr1::regex_search(buffer, res, rx);
ofstream myfile;
myfile.open ("proc.txt", ios::app);
for ( unsigned int i=0; i<res.size(); ++i)
{
std::cout << res[i] << std::endl ;
myfile << res[i] << "\n";
}
myfile.close;
}
}
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <process ID>", argv[0]);
return 1;
}
int pid;
sscanf_s(argv[1], "%i", &pid);
HANDLE process = OpenProcess(
PROCESS_VM_READ | PROCESS_QUERY_INFORMATION,
false,
pid);
find_locs(process);
return 0;
}