Tuesday, 15 April 2014

C++ File input with various variables -



C++ File input with various variables -

i trying create load function program. wrote save information. dont have preference whether maintain each competitor on same row looked simplest.

void gamehandler::writetofile() { ofstream file ("competitors.txt"); if(file.is_open()) { file<<this->mnrofcompetitors<<"\n"; for(int i=0; i<this->mnrofcompetitors; i++) { file<<this->mcompetitors[i].getname()<<"\n"; file<<this->mcompetitors[i].getdateofbirth()<<"\n"; file<<this->mcompetitors[i].getgender()<<"\n"; } file.close(); } else { cout<<"unable write file."<<endl; } }

but when have load in. people have wierdest tutorials handles simplest of cases. why need help.

i started off doesnt work because dont know how lineswitch in input.

void gamehandler::loadfromfile() { cleanup(); //erase current info , replace loaded data. string linestring; int lineint; bool linebool; ifstream file ("competitors.txt"); if(file.is_open()) { file>>this->mnrofcompetitors; this->mcapacity = this->mnrofcompetitors+5; this->mcompetitors = new duathlet[this->mcapacity]; for(int i=0; i<this->mnrofcompetitors; i++) { file>>linestring; this->mcompetitors[i].setname(linestring); //need lineswitch file>>lineint; this->mcompetitors[i].setdateofbirth(lineint); //need lineswitch file>>linebool; this->mcompetitors[i].setgender(linebool); //need lineswitch } file.close(); } else { cout<<"unable open file."<<endl; } }

what u think getline? if u have problem endlines u can get endline char('\n') like, illustration char x; file.get(x) -> '\n' taken. other solution ignore illustration file.ignore(666, '\n')

c++ file-io

No comments:

Post a Comment