?
Solved

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

Posted on 2011-03-07
8
Medium Priority
?
590 Views
Last Modified: 2013-12-14
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.

0
Comment
Question by:shaolinfunk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35057153
MFC has a collection class - CStringArray, specifically for storing numbers of CString objects.  That could be an alternative if you were interested.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 1600 total points
ID: 35057183
Hi extern,

with MFC I would suggest to use CStdioFile somhow like in this (untested) pseudo-code:

CStdioFile f;
if ( FALSE == f.Open( "C:\\students.txt", CFile::modeRead  | CFile::typeText ) )
{
 // error handling
 return;
}
std::vector< CString > names;
CString strName;
while( FALSE != f.ReadString( strName ) )
{
 names.push_back( strName );
}

Open in new window


Hope that helps,

ZOPPO
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35057411
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:shaolinfunk
ID: 35057613
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?
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 35057762
Not mixing MFC and STL - well you could do as I suggested initially and use the MFC collection.  To read from a file by MFC have a look at the code snippet provided by ZOPPO which uses MFC objects to read from a file directly into CString objects.  Then just add them to the MFC collection

eg.

CString s("hello");
CStringArray sArray;
sArray.Add(s);
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35057922
>>> ...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 ...
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 200 total points
ID: 35057958
a pure mfc solution is fine as well, though the collection classes of mfc are much less methodologically sound ...

using stl you open the file by

 
std::ifstream txtfile("C:/students.txt");

Open in new window

then read line by line

 
std::string strline;
  while (std::getline(txtfile, strline))
  {
        ...

Open in new window


and use push_back on your std::vector<std::string> > like in the code zoppo has posted.

Sara

0
 
LVL 35

Expert Comment

by:sarabande
ID: 35058008
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question