gpt4 book ai didi

C++ 逐行读取文件但行类型为 CString 或 TCHAR

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:06 25 4
gpt4 key购买 nike

我得到以下例子

    CString line[100];

//string line;
ifstream myfile (_T("example.txt"));
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}

那“行”怎么能存入类型为CString或TCHAR的值。我收到这样的错误:

error C2664: '__thiscall std::basic_ifstream >::std::basic_ifstream >(const char *,int)'

请帮助我:)

最佳答案

首先声明:

CString line[100];

定义了一个包含 100 个 CString数组:你确定要那个吗?

或者您可能只希望一个 CString 将每一行读入?

// One line
CString line;

您可以选择将行读入 std::string,然后将结果转换为 CString:

string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
// Convert from std::string to CString.
//
// Note that there is no CString constructor overload that takes
// a std::string directly; however, there are CString constructor
// overloads that take raw C-string pointers (e.g. const char*).
// So, it's possible to do the conversion requesting a raw const char*
// C-style string pointer from std::string, calling its c_str() method.
//
CString str(line.c_str());

cout << str.GetString() << '\n';
}
myfile.close();
}

关于C++ 逐行读取文件但行类型为 CString 或 TCHAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22910344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com