我得到以下示例
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类型。我收到这样的错误:
错误C2664:'__ thistall std :: basic_ifstream&gt; :: std :: basic_ifstream&gt;(const char *,int)'
请帮帮我:)。
答案 0 :(得分:2)
首先,这句话:
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();
}
答案 1 :(得分:0)
std::getline()
的第二个参数需要std::string
,因此请先使用std::string
,然后将其转换为CString
string str_line;
ifstream myfile (_T("example.txt"));
if (myfile.is_open())
{
while ( getline (myfile, str_line) )
{
CString line(str_line.c_str());
cout << line << '\n';
}
myfile.close();
}