V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
fkbd
V2EX  ›  问与答

c 语言中,当使用 strcat 连接 IP 地址的时候,会自动跳出循环怎么解决?

  •  
  •   fkbd · 2016-02-10 19:10:17 +08:00 · 1608 次点击
    这是一个创建于 3002 天前的主题,其中的信息可能已经有所发展或是发生改变。
    for(int i=0;i<10;i++){      
            char o1[]="reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" /v ProxyServer /d \"";
            char o2[]="\" /f";
            char ip[100][100]={"183.140.162.58","1.14.1.1"};
            strcat(o1,ip[i]);
            cout<<strcat(o1,o2)<<endl;
        }
    

    发现当数组中出现 ip : 183.140.162.58 之后会自动跳出循环

    5 条回复    2016-02-10 20:22:29 +08:00
    wujichao
        1
    wujichao  
       2016-02-10 19:31:56 +08:00   ❤️ 1
    http://baike.baidu.com/item/strcat
    百度百科说"d 和 s 所指内存区域不可以重叠且 d 必须有足够的空间来容纳 s 的字符串。"

    改成 char o1[1000] 试试?
    theoractice
        2
    theoractice  
       2016-02-10 19:32:28 +08:00   ❤️ 1
    你把常量字符区的内存地址赋给 strcat 了。
    自己另开个数组,换 sprintf 。
    fkbd
        3
    fkbd  
    OP
       2016-02-10 19:33:44 +08:00
    @wujichao
    @theoractice
    谢谢两位 改成 char o1[1000]后问题解决了
    yksoft1
        4
    yksoft1  
       2016-02-10 20:17:23 +08:00   ❤️ 2
    1 、这是 C++,不是 C 语言
    2 、为啥不使用 std::string
    3 、比起使用 reg 命令,为啥不直接使用 Windows API 访问注册表
    wizardforcel
        5
    wizardforcel  
       2016-02-10 20:22:29 +08:00 via Android   ❤️ 1
    你都用了 c++了还不用 std::string 。简直难以容忍。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2560 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:11 · PVG 21:11 · LAX 06:11 · JFK 09:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.