Solved

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

Posted on 2011-03-07
8
573 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 400 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 34

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
Independent Software Vendors: 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 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 50 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 34

Assisted Solution

by:sarabande
sarabande earned 50 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 34

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

Technology Partners: 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

Suggested Solutions

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

732 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