I have the following for loop:
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();
How can I keep all data of res[i] into and array or a buffer, then after the loop ends sort out the date, make it unique then print out and save to file (so I don’t have duplicate data)?
In my example my code outputs say some numbers like:
123456
123456
12345678
but I need only:
123456
12345678
My full code is as follows:
#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;
}
You could copy your data into a set and then write the content of the set in your file; something like: