How to print a CStringList in c++ using cout

I am new to C++. I was just going through some code and got this doubt.

The code had some CStringList defined somewhere. I wanted to print that to a file. So I wrote the below code

 CStringList *stringListDev;
 .....
 .....
 .....
 ..... 
 ofstream myfile("example.txt");
 for(int i=0; i<stringListDev->Count; i++)
 {
    myfile << stringListDev[i];
 }

I was getting an error saying << not defined on list or something. Please guide me how to write contents of a stringlist to a file in C++.

Upvotes: 0

Views: 5337

Answers (8)

crashmstr
crashmstr

Reputation: 28573

If this is really a question about the MFC CStringList, then the iterations are not correct (at least for MFC in Visual Studio 2010).

CStringList stringListDev;
// add to list
POSITION pos = stringListDev.GetHeadPosition();
while(pos != NULL)
{
    CString value = stringListDev.GetNext(pos);
    myfile << (LPCTSTR)value; // cast to use built in operator
}

As a side note: MFC is pretty archaic in structure and design compared to the standard library, and I would recommend to only use them if strictly needed for interfacing with other MFC code (CDialog, etc.)

Upvotes: 0

I got it myself. The code was very simple

   CStringList *stringListDev;
   .....
   .....
   .....
   ..... 
   ofstream myfile("example.txt");
   for(int i=0; i<stringListDev->Count; i++)
   {
      myfile << stringListDev->Strings[i];
   }

Thanks for all your responses!! You people rock !!

Upvotes: 0

Ragesh Chakkadath
Ragesh Chakkadath

Reputation: 1531

If it serves your purpose, you can use the CStringList::Serialize to write the contents of your object to a file. See the sample code:

CStringList csList;
csList.AddTail( L"A" ); // Filling sample data
csList.AddTail( L"B" );
csList.AddTail( L"C" );
CFile File( L"foo.txt", CFile::modeCreate | CFile::modeWrite ); // File opened using CFile object
CArchive Arch( &File, CArchive::store , MAX_PATH ); // Assign it to CArchive object
csList.Serialize( Arch ); // Call Serialize
Arch.Close();
File.Close();

This code write the contents of csList to foo.txt as binary data. You can load it to an object by similar way.

Reference

Serialization CArchive CFile

Upvotes: 1

Ragesh Chakkadath
Ragesh Chakkadath

Reputation: 1531

Let me rewrite your code like this.

CStringList stringListDev; // CStringList doesn't need to be a pointer.
 .....
 .....
 .....
 ..... 
 ofstream myfile("example.txt");
 for(int i=0; i<stringListDev.Count; i++)
 {
    myfile << stringListDev[i].GetBuffer(stringListDev[i].GetLength());
    stringListDev[i].ReleaseBuffer();
 }

CStringList class contains the list of CString objects which is another MFC class. for cout, I think you need an LPTSTR as input. So, use GetBuffer() function to get the same and don't forget to call ReleaseBuffer(). For more info, See the documentation

From my experience, Passing the GetBuffer with no arguments (default parameter is 0) also returns a pointer to string and you don't need to call ReleaseBuffer() as it returns the pointer to original string without any memory allocation. but I don't recommend that as it is not in the documentation.

Upvotes: 1

Ajay
Ajay

Reputation: 18431

CStringList class itself is a collection class, you don't want it to be pointer.

CStringList stringListDev;
ofstream myfile("example.txt"); 
for(int i=0; i<stringListDev.GetCount(); i++) 
{ 
  myfile << stringListDev[i]; 
}

Upvotes: 1

sarat
sarat

Reputation: 11140

CString and associated containers define their character type according to the UNICODE settings in the file. You can either stick to wofstream if your program always follows UNICODE character set. Otherwise, please enclose the definition as below. So you can make sure your program will compile in UNICODE and ASCII/MBCS character set.

#ifdef UNICODE
typedef ofstream tstream;
#else
typedef wofstream tstream;
#endif

tstream myfile("example.txt");

It is good to see about BOM characters and some caveats on reading/writing Unicode strings.

Upvotes: 1

red1ynx
red1ynx

Reputation: 3775

CStringList have GetAt(pos) method, which returns CString. To output CString to ofstream use CT2A.

myfile << CT2A(stringListDev->getAt(i));

And don't forget close file:

myfile.close();

UPD Also you can use std::wofstream.

wofstream myfile("example.txt");
myfile << (LPCTSTR) stringListDev->getAt(i);

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361672

Instead of writing your own class, you should use std::vector and std::string as:

#include <vector>
#include <string>

std::vector<std::string> stringArray;

stringArray.push_back("some string");
stringArray.push_back("some other string");
//..

for(size_t i = 0 ; i < stringArray.size(); i++ )
    std::cout << stringArray[i] << std::endl;

And you can also use myfile in place of std::cout in the above code.

You can also replace the for loop with this:

#include <iterator>
#include <algorithm>

std::copy(stringArray.begin(), stringArray.end(), 
          std::ostream_iterator<std::string>(myFile,"\n"));

Upvotes: 0

Related Questions