2014-04-07 5 views
2

입니다 I 다음의 예를 얻을C는 파일을 줄을 읽어 ++하지만 선 유형은 CString을하거나 TCHAR

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)'

+1

읽기 : 문자열 (표준 : : 문자열 라인) :

// One line CString line; 

당신이 가지고있는 옵션은 std::string에 줄을 읽은 다음 CString에 결과를 변환하는 것입니다 CString으로 변환하여 MSDN에서 제안하는 방법 - http://msdn.microsoft.com/en-us/library/ms235631(v=vs.80).aspx – SChepurin

+0

예를 들어 주시겠습니까? –

답변

2
로 변환 사용하십시오

첫째,이 진술 :

배열을 100 CString s로 정의합니다. 원하십니까?

또는 하나를CString 개로 각각 읽으시겠습니까? 다음과; 수 std

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(); 
} 
+0

오류가 발생했습니다 : 오류 C2039 : 'GetString': 'CString'의 멤버가 아닙니다. 어떻게 수정합니까? –

+0

@ MyrdaSahyuti : 어떤 컴파일러를 사용하고 있습니까 ?? 'GetString()'은 적어도 VS2005부터'CString' 멤버입니다. 어쨌든'cout << static_cast (str) << '\ n''을 시도해보십시오. –

+0

Visual C++ 6.0을 사용하고 있습니다. –

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(); 
} 
+0

감사합니다, 좋은 예도 –