0

I currently have this function to print out every rows of my tables

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;
}

How do I print out szColName, such that it only appear once on top, and not multiple occurences of it? Tried this:

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;
}

But it outputs everytime after a outputting the row values

  • But I don't think `sizeof(szColName)/sizeof(szColName[0])` gives `n`. dividing the size of two pointers probably gives you `1`. – phoeagon Feb 17 '13 at 10:33
  • You might print headers for the first time when the function is called (and regarding where to keep this "first time" flag, take a look at `void *NotUsed`). – Anton Kovalenko Feb 17 '13 at 10:34

1 Answers1

0

You might want to declare a static bool inside callback to record whether it has already printed out the column names. Or, if you want to be able to reset it...

such as:

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.
}

I assume that what you provide would print out the rows and the column names correctly. I only added a variable to check if printing column names are needed.

phoeagon
  • 2,080
  • 17
  • 20
  • Oh yes, that did the job, but `int n = sizeof(szColName) / sizeof(szColName[0]);` should be `int n = sizeof(szColName);` If I want to print all out of szColName. Nevertheless thanks! – Wong Chun Kiat Feb 17 '13 at 10:45
  • Oh wait, I discovered something, when I tried to execute again, the cols disappear. – Wong Chun Kiat Feb 17 '13 at 11:03
  • @WongChunKiat That's right. But actually I don't quite understand your specification. If you need to print the col names, change `firstline` into a global variable so that you can reset it. – phoeagon Feb 17 '13 at 11:42
  • @WongChunKiat No your edit isn't correct either. Basically there is no way to determine the number of elements from a `char**`. If `n` is the same as `argc`, then use `argc`. Otherwise you need another parameter. `sizeof(szColName)` merely gives the size of a pointer pointing to a pointer to a char ( and that's usually just the size of a pointer! ) The reason that `sizeof(szColName)` works for you is just by chance. – phoeagon Feb 17 '13 at 11:52