Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

How do I store a list of names into a vector of CStrings?

Hi,  

I've got a list of student names in C:\students.txt.  It looks like this:

Adam
Zach
Caroline
Dave
etc.

 I would like to store the names in a vector of CStrings.  Can someone show me how to do this?  Thanks.

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

MFC has a collection class - CStringArray, specifically for storing numbers of CString objects.  That could be an alternative if you were interested.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
normally you wouldn't mix MFC collection classes with STL objects. so i would suggest to using std::ifstream, std::getline, std::string and std::vector.

if you finally need to make a CString from a std::string it is simply like  CString(stdstr.c_str()).

Sara
Avatar of shaolinfunk

ASKER

ok i think sara pointed out something i didn't think, to not mix MFC and STL.  so, instead of using CString how can i accomplish what i want using only STL?

i'm familiar with vector and string...can you show me how to do the read/input part with ifstream and getline?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>> ...pointed out something i didn't think, to not mix MFC and STL
Well, IMO there's no problem with and nothing against mixing MFC and STL. If I i.e. implement a dialog application using MFC since it's easy to fast write dialog applications with MFC there's no reason not to use STL classes only because both MFC and STL have their own string and file classes.

So IMO it's a matter of taste and habits. I for myself use that class which best fits my current needs. But on the other hand I think if there's no real reason to prefer one of these one should choose MFC or STL in a consistent way since this can improve readability.

Anyhow, that's just my opinion ...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i don't mix these classes cause they have different concepts and interfaces. you necessarily will need to convert between equivalent classes what in my opinion is unnecessary and against object-orientation approach where each object has one class and not more.

Sara