Ehm naja das Problem ist halt, dass LPCTSTR für const TCHAR* steht ... TCHAR bei UNICODE für wchar_t und sonnst für char. std::string ist ja als std::basic_string<char> definiert. Also würde es, wenn du ein UNICODE-Projekt hast, Probleme geben. Hab es deswegen iwann einfach mal in ne Struct gepackt:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
//! struct-template for charset independent string operation
/*! The basic_char_types-structure template is used for charset independent source code */
template <typename T>
struct basic_char_types
{
typedef T character; //!< charset char-type
typedef std::basic_string<T> string; //!< char-type specification of std::basic_string
typedef std::basic_ostream<T, std::char_traits<T> > ostream; //!< char-type specification of std::basic_ostream
typedef std::basic_istream<T> istream; //!< char-type specification of std::basic_istream
typedef std::basic_ofstream<T> ofstream; //!< char-type specification of std::basic_ofstream
typedef std::basic_ifstream<T> ifstream; //!< char-type specification of std::basic_ifstream
typedef std::basic_ostringstream<T> ostringstream; //!< char-type specification of std::basic_ostringstream
typedef std::basic_istringstream<T> istringstream; //!< char-type specification of std::basic_istringstream
typedef std::basic_stringstream<T> stringstream; //!< char-type specification of std::basic_stringstream
typedef std::vector< std::basic_string<T> > string_array; //!< dynamic array of std::basic_string's specificated of char-type
};
#if _UNICODE
typedef basic_char_types<wchar_t> char_types; //!< typedef of basic_char_types depended of selected/defined charset
#define cout std::wcout
#else // !_UNICODE
typedef basic_char_types<char> char_types; //!< typedef of basic_char_types depended of selected/defined charset
#define cout std::cout
#endif // !_UNICODE
};
|
... das nutzt du jetzt einfach so:
|
C-/C++-Quelltext
|
1
2
3
4
|
char_types::ifstream file_stream("config.dat");
if (!file_stream) { std::cerr << "FEHLER: Datei konnte nicht geöffnet werden!" << std::endl; return 1; }
char_types::string line;
while (std::getline(file_stream, line)) { cout << line << std::endl; }
|
wobei cout jetzt eigentlich auch mit in die struct kann ... hab es gerade nur vergessen gehabt