Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-03-07
8
Medium Priority
?
601 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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 the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

636 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