简体   繁体   English

尝试执行ipconfig时,控制台system()c ++无限循环

[英]console system() c++ infinite loop while trying to do ipconfig

I'm currently learning c++ from scratch, I've previously developed apps with C# using Visual Studio but I'm a total noob with C++, I'm trying to make a small console exe that releases and renews the ip while practicing using headers and differents .cpp files. 我目前正在从零开始学习c ++,我以前使用Visual Studio用C#开发了应用程序,但是我对C ++完全陌生,我正在尝试制作一个小型控制台exe,以便在练习使用标头时发布和更新ip。和不同的.cpp文件。

The issue is that when I run the local windows debugger from visual studio 2015 the code runs perfectly and does everything I'm trying to. 问题是,当我从Visual Studio 2015运行本地Windows调试器时,代码可以完美运行,并且可以执行我尝试执行的所有操作。 But when I build and try to run the .exe file it goes into an infinite loop stating endlessly the output from the std::cout <<"Realizando ipconfig Release", I have localized the issue to when it tries to run the system("ipconfig /release"). 但是,当我构建并尝试运行.exe文件时,它陷入无限循环,不断声明std :: cout <<“ Realizando ipconfig Release”的输出,我已将问题本地化为尝试运行系统的时间( “ ipconfig / release”)。 Why does this happen? 为什么会这样? and How can I fix it? 以及如何解决?

This is the header 这是标题

#pragma once
#ifndef HeaderIp
#define HeaderIp
int Release();
int Renew();
#endif // !HeaderIp

This is the release.cpp 这是release.cpp

   #include <stdlib.h>
int Release()
{
    if (system(nullptr)==0)
    {
        return 0;
    }
    else
    {
        system("ipconfig /release");
        return 1;
    }
}

This is the renew.cpp 这是renew.cpp

#include <stdlib.h>;
int Renew()
{
    if (system(nullptr)==0)
    {
        return 0;
    }
    else
    {
        system("ipconfig /renew");
        return 1;
    }
}

and finally this is the ipconfig.cpp 最后是ipconfig.cpp

#include <iostream>
#include <stdlib.h>
#include "HeaderIp.h"
#include <stdio.h>

int main()
{   
    std::cout << "Realizando ipconfig Release" << std::endl;
    int i = 0;                                                      //Bit de informacion de status de CPU
    i = Release();
    if (i == 0)
    {
        std::cout << "Error al liberar IP" << std::endl;
        system("pause");
        exit;
    }
    else
    {
        std::cout << "Ip Liberado correctamente" << std::endl;
    }
    i = Renew();
    if (i == 0)
    {
        std::cout << "Error al renovar IP" << std::endl;
        system("pause");
        exit;
    }
    else
    {
        std::cout << "Ip renovado correctamente" << std::endl;
    }

system("pause");
return 0;

}

It's unusual, and indeed generally discouraged to use system in C++. 这是不寻常的,实际上通常不建议在C ++中使用system If you were to manage DHCP leases using IpReleaseAddress and IpRenewAddress , instead, you might find your problem disappears . 如果要使用IpReleaseAddressIpRenewAddress管理DHCP租约 ,则可能会发现问题消失了

You can use std::cin.sync() instead of system("pause") , too. 您也可以使用std::cin.sync()代替system("pause")

exit; probably doesn't do what you want it to, as you're only referring to the function, you're not calling it, so... it'll do nothing . 可能不执行您想要的操作,因为您仅引用该函数,而不是调用它,所以...它什么也不会做。

The semicolon in #include <stdlib.h>; #include <stdlib.h>;的分号#include <stdlib.h>; is an error, and you should be including <cstdlib> and <cstdio> rather than <stdlib.h> and <stdio.h> . 是一个错误,您应该包含<cstdlib><cstdio>而不是<stdlib.h><stdio.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM