Hello this is a program that finds player id's inside npc_heroes.txt file and prints them out. Im having problems my program stops after finding few player id's also sometimes i get bad alloc error, but compile with no problems. To run this program you will need to find npc_heroes.txt file in google search and download it and put it in the same folder as your program exe. Also you will need a key.txt file you will have to make it your self copy this text:
1 2 3
"
{
dont copy last char.!!!
Create new txt file and name it key and paste the text.
#include <iostream>
#include <fstream>
int main(){
//key is = "\"\r\n\t{\r\n\t";
std::streampos size2;
char *memblock2;
std::ifstream file2 ("key.txt", std::ios::in|std::ios::binary|std::ios::ate);
if(file2.is_open()){
size2 = file2.tellg();
memblock2 = newchar [size2];
file2.seekg(0, std::ios::beg);
file2.read (memblock2, size2);
file2.close();
std::cout << "The entire file2 content is in memory:\n";
}
else std::cout << "Unable to open file2";
std::streampos size1;
char *memblock;
std::ifstream file ("npc_heroes.txt", std::ios::in|std::ios::binary|std::ios::ate);
if (file.is_open())
{
size1 = file.tellg();
memblock = newchar [size1];
file.seekg (0, std::ios::beg);
file.read (memblock, size1);
file.close();
std::cout << "The entire file content is in memory:\n";
for(longlong i1=0,i2=0; i1<size1; ++i1){
if(i2 == size2){// When all the key characters is found inside memblock.
char *tempmem = memblock + i1 - size2 - 1;// this is a pointer to where last letter of the key was found.
int tempint = 0;// this is to find size of the payer id we want to find.
while(isalpha(*tempmem) || (*tempmem == '_')){--tempmem;++tempint;}//every time *tempmem is a letter or _ we will go back within tempmem and add one to tempint;
char *tempstr = newchar[tempint];// now we know how big is. So we create new char array of that size.
char *tempstr2 = tempstr;// create one more pointer to tempstr because i don't want to interact directly with tempstr now.
tempmem = tempmem + 1;// go forward within tempmem to be in the right place.
while(isalpha(*tempmem) || (*tempmem == '_')){
*tempstr2 = *tempmem;// here we coppy the player id to tempstr one by one char.
++tempmem;
++tempstr2;
}
tempstr[tempint] = '\0';// set last char inside tempstr to \0 so we can cout.
std::cout<<tempstr<<std::endl;// print player id.
delete[] tempstr;//must delete this now!
i2=0;// set this to 0 so we can continue to search for more player id's in file npc_heroes.txt
continue;
}
if(memblock[i1] != memblock2[i2]){// not found!
i2=0;
continue;
}
if(memblock[i1] == memblock2[i2]){// Found one of the key characters.
++i2;
continue;
}
}
}
else std::cout << "Unable to open file";
delete[] memblock;
delete[] memblock2;
char c;
std::cin >> c;// don't close window.
return 0;
}
> this is a program that finds player id's inside npc_heroes.txt file and prints them out
seems quite convoluted for something like that, you'll need to explain your logic.
> you will need a key.txt file
¿why? ¿how is being used?
> also sometimes i get bad alloc error, but compile with no problems.
you need to learn the difference between compile errors, linker errors, runtime errors and logic errors.
> you will need to find npc_heroes.txt file in google search
yeah, screw you
Line 36 allocates an array of tempint chars.
Line 44 sets the tempint+1'th char and your program goes BOOM.
Change line 36 to char *tempstr = newchar[tempint+1];
With this change I found 77 values in the copy of npc_heroes.txt that I found. But there are more entries in the file. The problem was that some of the names ended with \n\t{\t\t\t\t\t\t\n
You would be better off looking for "\t\"npc_dota" at the beginning of a line and using that as the beginning of the name. Then you can read the file line by line and the code is far simpler:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
int main()
{
std::string line;
while (std::getline(std::cin, line)) {
if (line.find("\t\"npc_dota") == 0) {
std::cout.write(line.c_str()+3, line.size()-4);
std::cout << std::endl;
}
}
}