?
Solved

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

Posted on 2011-03-07
8
Medium Priority
?
611 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 45

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
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!

 
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 45

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

840 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