尝试执行IPConfig时,Console System()C 无限循环



我目前正在从头开始学习C ,我以前使用C#使用Visual Studio开发了应用程序,但我是C 的总菜鸟,我正在尝试制作一个小型控制台exe使用标题和不同的练习时释放和续订IP。

问题是,当我从Visual Studio 2015运行本地Windows调试器时,代码运行完美,并且可以完成我尝试使用的一切。但是,当我构建并尝试运行.exe文件时,它将进入一个无限的循环,无休止地说出来自std :: cout<<<" realizando ipconfig realement"的输出,我已经将问题本地化为试图运行的问题系统(" ipconfig/preasure"(。为什么会发生这种情况?我该如何修复?

这是标题

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

这是版本。CPP

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

这是reenew.cpp

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

最后,这是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;
}

这是不寻常的,实际上通常不愿在C 中使用system。如果要使用IpReleaseAddressIpRenewAddress管理DHCP租赁,则可能会发现问题消失

您也可以使用std::cin.sync()而不是system("pause")

exit;可能不会做您想要的事情,因为您只将引用 ,而不是 datch 它,所以...它会做什么都不做

#include <stdlib.h>;中的分号是一个错误,您应该包括<cstdlib><cstdio>而不是<stdlib.h><stdio.h>

最新更新