#includeusing namespace std; int main() { string str = "Hello World!"; cout << str << endl; return 0; }
#include#include using namespace std; int main() { string str = "This is a text file demonstration in C++"; ofstream myfile; myfile.open("example.txt"); myfile << str; myfile.close(); return 0; }
#includeThis example shows how to declare a string variable and then use it to set the text of a Windows textbox. Package library: Windows API.LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG Msg; WNDCLASS wcl; wcl.hInstance = hInstance; wcl.lpszClassName = TEXT("Edit control"); wcl.lpfnWndProc = WndProc; wcl.style = 0; RegisterClass(&wcl); hwnd = CreateWindow(wcl.lpszClassName, TEXT("Edit Control"), WS_SYSMENU | WS_VISIBLE, 50, 50, 400, 200, NULL, NULL, hInstance, NULL); string str = "This is a text string in C++"; LPCSTR lpszText = str.c_str(); SetWindowText(hwnd, lpszText); while (GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; }