...
fin->seekg(0, ios::beg); //set to begining of file to count id names
int nNames = 0; //total amount of names listed
int nChar = 0;
while(fin->good()) { //reads chars until dash, checks for spaces to tally name count
char curr = fin->get();
nChar += 1;
if(curr == '-') //check if end line
break;
elseif(curr == ' ') //if a space then count as new name
nNames += 1;
}
//fin->seekg(0, ios::beg); //go back to the begining to read names
char** ids[nNames]; //will store array of names
int start = 0; //keeps track of starting index of the name
for(int i = 0; i < nNames; i++) {
fin->seekg(0, ios::beg); //go back to begining to read name
fin->ignore(start); //skip to start of name
int length = 0; //get current position of name char
while(fin->good()) {
char curr = fin->get();
if(curr != ' ' && curr != '-') {
length += 1; // count size of name
} else {
break;
}
}
if (length > 0) {
fin->seekg(0, ios::beg); //go back to begining to read name
fin->ignore(start); //skip to start of name
cout << "s: " << start << " l: " << length << " ";
start += length + 1; //set start for the next name char index in file
char* name = newchar[length+1]; //create one larger for termination character
fin->read(name,length);
name[length] = '\0'; //terminate name for proper display
*ids[i] = name; //HOW CAN I DEFINE THIS?
}
}
}
The last line where I try to read the chars from the file causes an issue. I think it is how I have my ids[] array declared?
Ah, I knew it was something incredibly simple and stupid I did. References are a love-hate thing with me. Thank you! Also, I found a much much simpler way of reading the names into a char[]!
fin->seekg(0, ios::beg); //set to begining of file to count id names
nIds = 0; //total amount of names listed
while(fin->good()) { //reads chars until dash, checks for spaces to tally name count
char curr = fin->get();
if(curr == '-') //check if end line
break;
elseif(curr == ' ') //if a space then count as new name
nIds += 1;
}
fin->seekg(0, ios::beg); //go back to the begining to read names
ids = newchar*[nIds]; //will store array of names
for(int i = 0; i < nIds; i++) {
char* name;
*fin >> name;
if(name[0] != '-') {
ids[i] = name;
cout << ids[i];
}
}