c++ - Extracting Data in different formats in a Text File -
i'm having issue reading in data , i've been researching past hour no better conclusion. pasted below snippets of code , file i'm reading form. file opened , struct declarations shown below well. i'm confident solution simple , i'm overthinking due working on project long here's explanation.
the data file gets opened , data gets read appropiate place until while(infile>>temp.startdistance.....) part. goal @ point read in zombies stated explicitly in each round after number of random zombies read( see data file format). in previous cases of reading in data, used getline, sscanf on c string version of string received getline extract appropiate data. since explicit zombies formatted white space separating data, wanted use >> operater extract data. i've read it's poor practice mix getline , believe in case make sense. there no point in running getline(),strcpy(), , sscanf() have o(n) complexity when 1 0(n) function (>>) suffice (this tested speed).
my issue reads in explicit zombies in round 1. however, when reaches "---" meaning new round start @ beginning of while loop, exits out of both loops entirely. i've researched flags , thought failbit occur , program counter go initial while loop read in new round. i've tried using peek() no success , shortcut make me fail test cases potentially.
i'm focusing on fixing final while loop rather redoing entire code listed below. if more information needed, i'll glad add more. !
struct zombiesperround{ unsigned int round; unsigned int numzombies; }; struct zombie{ unsigned int startdistance; unsigned int speed; unsigned int health; string name; }; string line; vector <zombiesperround> randomzombies; list <zombie> masterlist; ifstream infile(inputfile); if(infile.is_open()){ //read in player information getline(infile,line); char *read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"quiver_capacity: %u",&settings.numarrows); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"random_seed: %u",&settings.randomseed); srand(settings.randomseed); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"max_rand_distance: %u",&settings.maxdistance); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"max_rand_speed: %u",&settings.maxspeed); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"max_rand_health: %u",&settings.zombiehealth); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"player_health: %u",&settings.playerhealth); delete read; while(!infile.fail()){ getline(infile,line); if(!line.substr(0,1).compare("-")) continue; zombiesperround randzombie; read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"round: %u",&randzombie.round); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"num_zombies: %u",&randzombie.numzombies); delete read; randomzombies.push_back(randzombie); zombie temp; //read in explicit zombies while(infile >> temp.startdistance >> temp.speed >> temp.health >> temp.name){ masterlist.push_back(temp); } } } infile.close(); //test1.txt quiver_capacity: 10 random_seed: 2049231 max_rand_distance: 50 max_rand_speed: 60 max_rand_health: 1 player_health: 10 --- round: 1 num_zombies: 25 150 300 15 foxmccloud 2 3 6 falcolombardi 100 1 100 slippytoad --- round: 3 num_zombies: 50 20 10 20 darklink
it seems reading in 2 different ways (the getline , >>) bad input stream (something don't quite understand honest).
i've fixed problem this:
while(!infile.fail()){ getline(infile,line); if(!line.substr(0,1).compare("-")) getline(infile,line); zombiesperround randzombie; read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"round: %u",&randzombie.round); delete read; getline(infile,line); read=new char[line.length()+1]; strcpy(read,line.c_str()); sscanf(read,"num_zombies: %u",&randzombie.numzombies); delete read; randomzombies.push_back(randzombie); zombie temp; //read in explicit zombies while(getline(infile,line)){ if(!line.substr(0,1).compare("-")) break; stringstream ss(line); ss >> temp.startdistance >> temp.speed >> temp.health >> temp.name; masterlist.push_back(temp); infile.clear(); } }
} infile.close();
i'm pretty sure there more elegant / efficient ways it, tested file posted , seems work though.
hope helps..
Comments
Post a Comment