why cout output memory address not string? [duplicate] - c++

This question already has answers here:
How to print array of LPCTSTR in c++?
(4 answers)
How to initialize and print a std::wstring?
(6 answers)
Closed 3 years ago.
i am tring to use c++ to get display device info ,but cout give me some memory address ,how can i get string value ? output like :00CFF4B4:00CFF5F8,
i am using visual studio 2019
#include <iostream>
#include <windows.h>
int main()
{
DISPLAY_DEVICE d;
d.cb = sizeof(DISPLAY_DEVICE);
int device_num = 0;
while (EnumDisplayDevices(NULL, device_num, &d, 0))
{
std::cout << d.DeviceName << ":" << d.DeviceID<<std::endl;
device_num++;
}
return 0;
}

I'm assuming you're using the W version of DISPLAY_DEVICE (DISPLAY_DEVICEW) which I believe is the default since at least Visual Studio 2015 and later (the compiler defines UNICODE).
The DeviceName and DeviceID members of DISPLAY_DEVICEW are WCHAR (a macro for wchar_t), so you need to use wcout instead of cout:
std::wcout << d.DeviceName << ":" << d.DeviceID << std::endl;

Related

Disproving Determinism via c++ [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
C/C++ changing the value of a const
(18 answers)
Closed 5 months ago.
friends.
Recently I experimented a bit a C++ constants.
The code is:
#include <iostream>
int main() {
const int c = 1;
const int* ptr = &c;
int* tmp = const_cast<int*>(ptr);
*tmp = 5;
std::cout << &c << " " << ptr << " " << tmp << "\n";
std::cout << c << " " << *ptr << " " << *tmp;
}
I have investigated assembly code at godbolt: https://godbolt.org/z/7e3o7bWrs
The assembly code seems like doing what I wrote, exactly moving address of c into tmp variable and changes variable at this address.
Can you please tell me, why there is could be two different values at the same addresses?
Thank you.

Mixing std::wcout and std::cout makes errors, what is wrong? [duplicate]

This question already has answers here:
Mixing cout and wcout in same program
(6 answers)
Closed 3 years ago.
Having been compiled using g++, the program below prints the std::wcout expression only. But if you uncomment the 8th row, it prints three expressions properly.
I would like to know the cause of such strange behavior.
#include <iostream>
#include <cstring>
#include <boost/format.hpp>
int main () {
int x = 10;
wchar_t str[] = L"Hello, world!";
// std::cout << "what?" << std::endl;
std::wcout << L"str = \"" << str << L"\" | len = " << wcslen(str) << L"\n";
std::cout << boost::format("x = %d | &x = %p") % x % &x << std::endl;
return 0;
}
Quoting from this page
A program should not mix output operations on cout with output operations on wcout (or with other wide-oriented output operations on stdout): Once an output operation has been performed on either, the standard output stream acquires an orientation (either narrow or wide) that can only be safely changed by calling freopen on stdout.
The reason it works when you use cout first is because your implementation allows wcout to output to byte-oriented streams. This is not guaranteed for all implementations. As mentioned in the quoted text, the only correct way to switch between them is with freopen like so:
#include <cstdio>
#include <iostream>
int main () {
std::wcout << L"Hello" << std::flush;
freopen(nullptr, "a", stdout);
std::cout << " world\n" << std::flush;
}
But it's probably simpler to just avoid mixing them.

C++ - DWORD Decimal to DWORD Hexadecimal [duplicate]

This question already has answers here:
C++ cout hex values?
(10 answers)
Closed 5 years ago.
How can you convert a decimal to hexadecimal in C++? I've had success with ultoa but that gives you a char (and using (DWORD)Buffer to std::cout just ends in gibberish). Also i need to save the new hexadecimal in a DWORD again.
My Code so far:
//Vars
char Buffer[33];
// Client.dll
DWORD d_clientDll = (DWORD)GetModuleHandleA("client.dll");
_ultoa(d_clientDll, Buffer, 16);
std::cout << Buffer << std::endl;
std::cout << d_clientDll << std::endl;
Did you try std::hex ?
std::cout << std::hex << d_clientDll << std::endl

Writing array into text file using c++ [duplicate]

This question already has answers here:
How to output array of doubles to hard drive?
(6 answers)
Closed 6 years ago.
Here is the snippet of code that prints the output
void PrintFunc(double Y[])
{
for(int i=0 ; i<=N+1 ; i++)
{
cout << x_min+i*r << "\t" << Y[i] << endl;
}
}
I want to write it to a text file, how do I do it? i ranges from 1 to 2000.
#include <fstream>
std::ofstream ofs("filename.txt")
// then, in the loop:
ofs << ... << std::endl;

How to print a variable's name in C++? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Programmatic way to get variable name in C?
I have checked some of the blogs before posting this here. I have tried the following snippet...
int a=21;
int main()
{
cout<<#a<<a<<endl;
return 0;
}
I am using g++ compiler on ubuntu 10.04. And I am getting the following error:
sample.cpp:17: error: stray ‘#’ in program.
Please suggest me how to print the variables name .
The # stringifying macro thing only works inside macros.
You could do something like this:
#include <iostream>
#define VNAME(x) #x
#define VDUMP(x) std::cout << #x << " " << x << std::endl
int main()
{
int i = 0;
std::cout << VNAME(i) << " " << i << std::endl;
VDUMP(i);
return 0;
}
The # is for if you are writing a macro.
If your cout line were a macro, it would work the way you expect.
If you're not in a macro, you just type "a".