Database::Database(string fileName) { dataFile = fileName; ifstream someFile; someFile.open(fileName.c_str()); string data; list<ReadPerson> myReadPersons; while (!someFile.eof()) { getline(someFile, data); if (data[0] == '#') { continue; } int countCommas = 0; int lastComma = 0; string member, name; string gender; int yearOfBirth; int yearOfDeath; for (int i = 0; i < data.size(); i++) { if (data[i] == ',') { member = data.substr(lastComma, (i - lastComma)); lastComma = i + 2; if (countCommas == 0) { name = member; } else if (countCommas == 1) { gender = member; } else if (countCommas == 2) { yearOfBirth = atoi(member.c_str()); } else if (countCommas == 3) { yearOfDeath = atoi(member.c_str()); } countCommas++; } } member = data.substr(lastComma, data.size()+1); ReadPerson tempReadPerson = ReadPerson(name, gender, yearOfBirth, yearOfDeath); myReadPersons.push_back(tempReadPerson); } personList = myReadPersons; someFile.close(); };
void main() { char *filename = "char.txt"; FILE *file = fopen(filename, "r+"); if(!file){ printf("file %s not found\n", filename); getchar(); getchar(); return; } char c = 'q'; person p; //read file do { p = ReadPerson(file); persons.push_back(p); } while (!feof(file)); //print to screen for(unsigned float i = 0; i < persons.size(); i++){ printf("\n\n%i\n%s\n%i\n%s\n%i\n%i\n%i\n%i\n%s", persons[i].ID, persons[i].name, persons[i].health, persons[i].w.type, persons[i].w.damage, persons[i].w.armor, persons[i].position[0], persons[i].position[1], persons[i].taunt); } //edit float id = 0; while (id < 5){ printf("\nEdit?"); scanf("%i", &id); getchar(); if(id <4){ system("cls"); printf("\n\n%i\n%s\n%i\n%s\n%i\n%i\n%i\n%i\n%s", persons[id].ID, persons[id].name, persons[id].health, persons[id].w.type, persons[id].w.damage, persons[id].w.armor, persons[id].position[0], persons[id].position[1], persons[id].taunt); EditPerson(persons[id]); } } //save to file SavePerson(file); fclose(file); }