How to get the IPv4 address on windows in C++

1 Answer

0 votes
#include <iostream>
#include <fstream>

int main() {
	std::string line;
	std::ifstream IPFile;
	// ipconfig:  IPv4 Address. . . . . . . . . . . : 101.11.1.111
	char IPv4Line[] = "IPv4 Address. . . . . . . . . . . :";  
        
	system("ipconfig > ip.txt");

	IPFile.open("ip.txt"); 
	if (IPFile.is_open()) {
		while(!IPFile.eof()) {
			getline(IPFile, line);
			if (line.find(IPv4Line, 0) != std::string::npos) {
				line.erase(0, 39);
				std::cout << line << "\n";
				IPFile.close();
			}
		}
	}
	return 0;
}



/*
run:

101.11.1.111

*/

 



answered Apr 8, 2021 by avibootz

Related questions

1 answer 233 views
233 views asked Apr 8, 2021 by avibootz
1 answer 156 views
1 answer 201 views
1 answer 151 views
151 views asked Apr 18, 2021 by avibootz
1 answer 257 views
...