View Single Post
Old 10-18-2004, 09:16 AM   #2 (permalink)
formerScientist
Upright
 
Location: Milwaukee
Careful, do the names contain white space? When using the extraction operator (>>) it will read everything until the next chunk of whitespace. So if your file looks like:

John Doe
Mary Smith

Your vector will contain 4 elements:
vec[0] == "John"
vec[1] == "Doe"
vec[2] == "Mary"
vec[3] == "Smith"

if you want the newline to represent your delimeter you'll have to use the std::getline function to read a line of text at a time. Example:

std::ifstream fin("input.txt");

std::string person;
std::vector<std::string> names;
while (std::getline(fin, person))
{
names.push_back(person);
}
formerScientist is offline  
 

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