Need help with program.

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.

Here is my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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 = new char [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 = new char [size1];
    file.seekg (0, std::ios::beg);
    file.read (memblock, size1);
    file.close();
    std::cout << "The entire file content is in memory:\n";

    for(long long 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 = new char[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;
}

Thank you for all your help.
Last edited on
> 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 = new char[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;
	}
    }
}


$ ./foo < npc_heroes.txt
pc_dota_hero_base
pc_dota_hero_antimage
pc_dota_hero_axe
pc_dota_hero_bane
pc_dota_hero_bloodseeker
pc_dota_hero_crystal_maiden
pc_dota_hero_drow_ranger
pc_dota_hero_earthshaker
pc_dota_hero_juggernaut
pc_dota_hero_mirana
pc_dota_hero_nevermore
pc_dota_hero_morphling
pc_dota_hero_phantom_lancer
pc_dota_hero_puck"											
pc_dota_hero_pudge
pc_dota_hero_razor
pc_dota_hero_sand_king
pc_dota_hero_storm_spirit
pc_dota_hero_sven
pc_dota_hero_tiny
pc_dota_hero_vengefulspirit
pc_dota_hero_windrunner
pc_dota_hero_zuus
pc_dota_hero_kunkka
pc_dota_hero_lina
pc_dota_hero_lich
pc_dota_hero_lion
pc_dota_hero_shadow_shaman
pc_dota_hero_slardar
pc_dota_hero_tidehunter
pc_dota_hero_witch_doctor"
pc_dota_hero_riki
pc_dota_hero_enigma"
pc_dota_hero_tinker"
pc_dota_hero_sniper"
pc_dota_hero_necrolyte"
pc_dota_hero_warlock"
pc_dota_hero_beastmaster"
pc_dota_hero_queenofpain"
pc_dota_hero_venomancer"
pc_dota_hero_faceless_void
pc_dota_hero_skeleton_king
pc_dota_hero_death_prophet
pc_dota_hero_phantom_assassin
pc_dota_hero_pugna
pc_dota_hero_templar_assassin
pc_dota_hero_viper
pc_dota_hero_luna
pc_dota_hero_dragon_knight
pc_dota_hero_dazzle
pc_dota_hero_rattletrap
pc_dota_hero_leshrac
pc_dota_hero_furion
pc_dota_hero_life_stealer
pc_dota_hero_dark_seer
pc_dota_hero_clinkz
pc_dota_hero_omniknight
pc_dota_hero_enchantress
pc_dota_hero_huskar
pc_dota_hero_night_stalker
pc_dota_hero_broodmother
pc_dota_hero_bounty_hunter
pc_dota_hero_weaver"
pc_dota_hero_jakiro"
pc_dota_hero_batrider"
pc_dota_hero_chen"
pc_dota_hero_spectre"
pc_dota_hero_doom_bringer"
pc_dota_hero_ancient_apparition"
pc_dota_hero_ursa"
pc_dota_hero_spirit_breaker"
pc_dota_hero_gyrocopter"
pc_dota_hero_alchemist"
pc_dota_hero_invoker
pc_dota_hero_silencer"
pc_dota_hero_obsidian_destroyer"
pc_dota_hero_lycan"
pc_dota_hero_brewmaster"
pc_dota_hero_shadow_demon
pc_dota_hero_lone_druid"
pc_dota_hero_chaos_knight"
pc_dota_hero_meepo"
pc_dota_hero_treant"
pc_dota_hero_ogre_magi
pc_dota_hero_undying
pc_dota_hero_rubick
pc_dota_hero_disruptor
pc_dota_hero_nyx_assassin
pc_dota_hero_naga_siren
pc_dota_hero_keeper_of_the_light
pc_dota_hero_wisp
pc_dota_hero_visage
pc_dota_hero_slark
pc_dota_hero_medusa
pc_dota_hero_troll_warlord
pc_dota_hero_centaur
pc_dota_hero_magnataur
pc_dota_hero_shredder
pc_dota_hero_bristleback
pc_dota_hero_tusk
pc_dota_hero_skywrath_mage
pc_dota_hero_abaddon
pc_dota_hero_elder_titan
pc_dota_hero_legion_commander
pc_dota_hero_ember_spirit
pc_dota_hero_earth_spirit
pc_dota_hero_abyssal_underlord
pc_dota_hero_terrorblade
pc_dota_hero_phoenix
pc_dota_hero_oracle
pc_dota_hero_techies
pc_dota_hero_winter_wyvern

Thank you dhayden for your answer line 36 fix worked!!!
Topic archived. No new replies allowed.