【问题标题】:too many arguments to function 'FILE* popen()' in C++C++ 中函数 'FILE* popen()' 的参数太多
【发布时间】:2017-10-01 21:57:58
【问题描述】:

我写了一个小测试程序,当使用 GCC 编译时,它可以按预期编译和运行:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFSZ 512

int ping_test(char *host);

int main(void) {
    if (ping_test("192.168.1.1"))
        printf("PING OK!\n");
    else
        printf("PING FAIL!\n");



}

// returns 1 if ping succeeds, 0 otherwise
int ping_test(char *host)
{
    FILE *in;
    int i = 0;
    int ok = 1;
    extern FILE *popen();
    char buff[BUFFSZ] = {0};
    char res[BUFFSZ] = {0};
    char cmd[50]={0};

    sprintf(cmd,"ping -c 1 %s",host);
    if(!(in = popen(cmd, "r"))){
            exit(1);
    }
    while(fgets(buff, sizeof(buff), in)!=NULL){
            strcat(res,buff);
    }   
    pclose(in);
    printf("RESULT: %s\n",res);
    // check for string "100%"
    for (i=0;i<BUFFSZ;i++) {
        if (res[i] == '\%' && 
            res[i-1] =='0' &&
            res[i-2] =='0' &&
            res[i-3] =='1'){
            ok = 0;
        return ok;
        } else
            ok = 1;
    }
    return ok;
}

现在我想在我正在处理的 Qt C++ 程序中使用这个相同的函数,所以我将该函数复制到我的类中的私有方法中:

int JTFTestSuite::Ping(char *host)
{
    FILE *in;
    int i = 0;
    int ok = 1;
    extern FILE *popen();
    char buff[BUFFSZ] = {0};
    char res[BUFFSZ] = {0};
    char cmd[BUFFSZ]={0};

    sprintf(cmd,"ping -c 1 %s",host);
    if(!(in = popen(cmd, "r"))){
            exit(1);
    }
    while(fgets(buff, sizeof(buff), in)!=NULL){
            strcat(res,buff);
    }
    pclose(in);
    printf("RESULT: %s\n",res);
    // check for string "100%"
    for (i=0;i<BUFFSZ;i++) {
        if (res[i] == '\%' &&
            res[i-1] =='0' &&
            res[i-2] =='0' &&
            res[i-3] =='1'){
            ok = 0;
        return ok;
        } else
            ok = 1;
    }
    return ok;
}

标题中的声明:

private:
    int Ping(char *host);

但是现在,它没有编译,编译器抱怨:

error: too many arguments to function 'FILE* popen()'
     if(!(in = popen(cmd, "r"))){
                             ^

为什么我想知道这个? 两个源文件中的包含相同

【问题讨论】:

  • 如果你摆脱 extern FILE *popen(); 会发生什么?
  • 在 C++ 中,您的前向声明必须是完整的。 extern FILE *popen(); 声明 popen 接受 0 个参数,然后你用 2 调用它。这将被拒绝。要么包含正确的标题,要么正确地声明它
  • @MikeVine 谢谢,如果您将此评论移到答案中,我会接受它作为解决方案!
  • 包括相关的头文件,不要自己重新声明库函数。

标签: c++ gcc popen


【解决方案1】:

在 C 中,您可以通过使用空参数列表来声明函数而不声明任何特定原型。 (你不应该这样做。它已经被弃用了很长时间。但你可以。)如果你想声明一个不带参数的函数,你需要使用void作为参数列表。

这在 C++ 中不起作用。在 C++ 中,空参数列表意味着函数不接受任何参数。

所以在 C 中,

FILE* popen();  // unspecified what the parameters are
FILE* f = popen(cmd, "r");   // called with two strings

在 C++ 中

FILE* popen();  // takes no arguments
FILE* f = popen(cmd, "r");   // illegal because popen takes no argument.

而不是自己声明popen(和其他库函数),#include 是正确的头文件。 popenpclose&lt;stdio.h&gt; 中,但您需要确保在任何 #includes 之前定义正确的 feature test macro。 (例如,#define _XOPEN_SOURCE 700 会起作用。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2021-01-17
    • 2017-04-04
    相关资源
    最近更新 更多