0

I'm trying to use a system call to print out my IP address, instead of calling a batch script. My current code is:

#include <iostream>
#include <string>

void main(){
system("@echo off");
system("ipconfig | findstr /R /C:'IPv4 Address'");
system("PAUSE");
return;
}

However when ran I get this error "FINDSTR: Cannot open Address'". What am I doing wrong?

  • 1
    The effect of `@echo off` will expire when the shell exits; multiple calls to `system()` are not an effective substitute for a batch file. – Ben Voigt Aug 08 '16 at 19:53
  • 1
    Why are you using the shell and an external program for this at all, when you could just call the function that returns this information? – Ben Voigt Aug 08 '16 at 19:53
  • Does the command work when you type it into the console? Those `'` look like they should be `"`. – NathanOliver Aug 08 '16 at 19:54
  • @BenVoigt I couldn't find a function that returns the information. – CipheredData Aug 08 '16 at 19:56
  • On linux it's `getifaddrs`, on Windows see http://stackoverflow.com/a/10838854/103167 – Ben Voigt Aug 08 '16 at 19:57
  • @NathanOliver If you use `"` instead of `'` then the string ends at the start of "IPv4 Address" and starts again at the end of it. – CipheredData Aug 08 '16 at 19:58
  • 2
    @CipheredData: You would escape it as `\"` in the C++ source code, resulting in an actual double quote character in the string sent to `system()`. But `system()` is the wrong way to do this to begin with. – Ben Voigt Aug 08 '16 at 19:59
  • @BenVoigt Thanks. :) – CipheredData Aug 08 '16 at 19:59

0 Answers0