0

我目前有这个功能来打印我的表格的每一行

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
    for(int i = 0; i < argc; i++)
    {
        cout.width(17); cout << left << argv[i];
    }

    std::cout << "\n";

    return 0;
}

如何打印出 szColName,使其仅在顶部出现一次,而不是多次出现?试过这个:

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
    int n = sizeof(szColName) / sizeof(szColName[0]);
    for (int i = 0; i < n; i++)
    {
        cout.width(17); cout << left << szColName[i];
    }
    printf("\n");
    for(int i = 0; i < argc; i++)
    {
        cout.width(17); cout << left << argv[i];
    }

    std::cout << "\n";

    return 0;
}

但它每次在输出行值后输出

4

1 回答 1

0

您可能想要声明一个static bool内部回调来记录它是否已经打印出列名。或者,如果您希望能够重置它...

如:

static bool firstline = true;

static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
if (firstline){
    int n = sizeof(szColName) / sizeof(szColName[0]);//this is incorrect but fixing
                                                 // it requires changing the prototype. 
                                                  //See the comments below
    for (int i = 0; i < n; i++)
    {
        cout.width(17); cout << szColName[i] << left;
    }
    printf("\n");
    firstline=false;
}
for(int i = 0; i < argc; i++)
{
    cout.width(17); cout << argv[i] << left;
}

std::cout << "\n";

return 0;
}
int main(){
    for(int x=0;x<10;++x)callback( ... , ... , ... ); // give whatever argument you need to give

    firstline = true;  //reset the variable so that next time you call it, the col names will appear
    for(int x=0;x<10;++x)callback(...,...,...);// now the col names will appear again.
}

我假设您提供的内容会正确打印出行和列名。我只添加了一个变量来检查是否需要打印列名。

于 2013-02-17T10:36:41.733 回答