如何使用cout在c ++中打印CStringList

时间:2011-07-28 08:15:09

标签: c++ string mfc

我是C ++的新手。我只是通过了一些代码并得到了怀疑。

代码在某处定义了一些CStringList。我想将它打印到文件中。所以我写了下面的代码

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

我收到错误说&lt;&lt;没有在列表或其他东西上定义。 请指导我如何使用C ++将字符串列表的内容写入文件。

8 个答案:

答案 0 :(得分:1)

CString和关联容器根据文件中的UNICODE设置定义其字符类型。如果您的程序始终遵循UNICODE字符集,则可以坚持使用wofstream。否则,请附上以下定义。因此,您可以确保您的程序将使用UNICODE和ASCII / MBCS字符集进行编译。

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

tstream myfile("example.txt");

很高兴看到BOM字符以及读取/写入Unicode字符串的一些注意事项。

答案 1 :(得分:1)

CStringList类本身就是一个集合类,你不希望它成为指针。

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

答案 2 :(得分:1)

让我像这样重写你的代码。

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类包含CString个对象的列表,这是另一个MFC类。对于cout,我认为您需要LPTSTR作为输入。因此,使用GetBuffer()函数来获取相同内容,不要忘记调用ReleaseBuffer()。有关详细信息,请参阅the documentation

根据我的经验,传递没有参数的GetBuffer(默认参数为0)也会返回一个指向string的指针,你不需要调用ReleaseBuffer(),因为它返回指向原始字符串的指针而没有任何内存分配。但我不建议这样做,因为它不在文档中。

答案 3 :(得分:1)

如果它符合您的目的,您可以使用CStringList::Serialize将对象的内容写入文件。 请参阅示例代码:

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();

此代码将csList的内容写为foo.txt二进制数据。您可以通过类似的方式将其加载到对象。

参考

Serialization CArchive CFile

答案 4 :(得分:0)

您应该使用std::vectorstd::string作为:

,而不是编写自己的课程。
#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;

您还可以在上面的代码中使用myfile代替std::cout

您也可以使用以下代码替换for循环:

#include <iterator>
#include <algorithm>

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

答案 5 :(得分:0)

CStringListGetAt(pos)方法,返回CString。 要将CString输出到ofstream,请使用CT2A

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

不要忘记关闭文件:

myfile.close();

<强> UPD 您也可以使用std::wofstream

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

答案 6 :(得分:0)

我自己得到了它。代码非常简单

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

感谢您的所有回复!!你们摇滚!!

答案 7 :(得分:0)

如果这确实是关于MFC CStringList的问题,则迭代不正确(至少对于the guide)。

<?php 
include './DbHandler.php';
$db = new DbHandler();

$response = array();

if ((isset($_POST['profile_image']) && isset($_POST['username']) &&       isset($_POST['businessname']) && isset($_POST['town']))!= '') {
    $profile_image = $_POST['profile_image'];
    $username = $_POST['username'];
    $businessname = $_POST['businessname'];
    $town = $_POST['town'];

    $res = $db->createProfile($profile_image, $username, $businessname,$town);

    if ($res == USER_CREATED_SUCCESSFULLY) {      
        $response["error"] = false;
        $response["message"] = "Profile created successfully";
    } else if ($res == USER_CREATE_FAILED) {
        $response["error"] = true;
        $response["message"] = "Sorry! Error occurred during profile creation.";
    } 
} 
?>

作为旁注:与标准库相比,MFC在结构和设计方面相当陈旧,我建议仅在严格需要与其他MFC代码(CDialog等)连接时才使用它们。 。)