0
#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>
#include <string>
#include <format>


using namespace std;

#pragma warning(disable:4996)

int main() {
    char* p = getenv("USERPROFILE");
    const char* dllpath = "C:\\Users\\{here i need to write p value but how?}\\Desktop\\ExterTi\\req\\sk.dll"; // here
    //cout << dllpath << endl;
    system("pause");

    

}

how to format values like python in c++? like ...

ip = "192.168.2.1"
print(f"ip => {ip}")   # output ip => 192.168.2.1

i mean i need to write customer's current c:\Users{currentUser} i got it but i don't know how do i write this value

Kanot
  • 9
  • 3
  • 1
    Use either [C++20 STL Formatting Library](https://en.cppreference.com/w/cpp/utility/format/format) or [Boost.Format](https://www.boost.org/doc/libs/1_77_0/libs/format/doc/format.html) – Ghasem Ramezani Aug 15 '21 at 19:47
  • 3
    @GhasemRamezani Don't forget [libfmt](https://github.com/fmtlib/fmt). – HolyBlackCat Aug 15 '21 at 19:50
  • Yeah, I'd replace boost with libfmt given that C++20's implementation is somewhat anemic and almost purpose defeating by still requiring `std::cout`. – sweenish Aug 15 '21 at 21:35

1 Answers1

0
ip = "192.168.2.1"
print(f"ip => {ip}")   # output ip => 192.168.2.1

Please try storing these in the string data-type instead of char* for easier manipulation capabilities

Now assuming you have

string ip = "192.168.2.1";

If you wish to print this, the simplest solution could be using the printf function to print your string, you could try

printf("ip => %s\n", ip);

Of course, do not forget to

#include <stdio.h>

or

#include <cstdio>

You could also just use

cout<<"ip => "<<ip<<endl;

For printing formatted data to string,

sprintf(dllpath, "C:\\Users\\%s\\Desktop\\ExterTi\\req\\sk.dll", p);

should work, given dllpath is not a constant and initialized with an initial size, eg char dllpath[500];

  • i want to do: const char* dllpath = "C:\\Users\\{here i need to write p value}\\Desktop\\ExterTi\\req\\sk.dll"; – Kanot Aug 15 '21 at 20:09
  • ```sprintf(dllpath, "C:\\Users\\%s\\Desktop\\ExterTi\\req\\sk.dll", p);``` You would need to ensure dllpath is not constant initially and has an initial size alloted, otherwise again, please use strings instead of char* – Harshit Dhawan Aug 15 '21 at 20:31