Solved

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

Posted on 2011-03-07
8
544 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 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 30

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 32

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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 30

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 32

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 32

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now