2013-02-16 63 views
-1

我有以下回調函數。回調函數的格式化輸出

// This is the callback function to display the select data in the table 
static int callback(void *NotUsed, int argc, char **argv, char **szColName) 
{ 
    for(int i = 0; i < argc; i++) 
    { 
     std::cout << " = " << argv[i] << std::endl; 
    } 

    std::cout << "\n"; 

    return 0; 
} 

// prepare our sql statements 
const char *pSQL[6]; 
pSQL[0] = "SELECT * FROM User"; 

// execute sql 
for(int i = 0; i < 1; i++) 
{ 
    rc = sqlite3_exec(db, pSQL[i], callback, 0, &szErrMsg); 
    if(rc != SQLITE_OK) 
    { 
     std::cout << "SQL Error: " << szErrMsg << std::endl; 
     sqlite3_free(szErrMsg); 
     break; 
    } 
} 

產生以下輸出

1 
mark 
[email protected] 
98989898 
mark 
mark 
ADMN 
U 
Where are you studying 
SIM 
0 

2 
ken 
[email protected] 
92222898 
ken 
ken 
ADMN 
U 
Where are you studying 
SIM 
0 

3 
joseph 
[email protected] 
92333718 
joseph 
joseph 
ADMN 
U 
Where are you studying 
SIM 
0 

4 
ricky 
[email protected] 
81822801 
ricky 
ricky 
ADMN 
U 
Where are you studying 
SIM 
0 

5 
ck 
[email protected] 
94567828 
ck 
ck 
ADMN 
U 
Where are you studying 
SIM 
0 

我需要它被水平這樣代替格式化的豎直:

1 mark  [email protected]  98989898 mark  mark  ADMN U Where are you studying SIM 0 
2 ken  [email protected]  92222898 ken  ken  ADMN U Where are you studying SIM 0 
3 joseph [email protected] 92333718 joseph joseph ADMN U Where are you studying SIM 0 
4 ricky  [email protected]  81822801 ricky  ricky  ADMN U Where are you studying SIM 0 
5 ck  [email protected]  94567828 ck  ck  ADMN U Where are you studying SIM 0 

我試圖在回調函數使用該:

1) std::cout << argv[i] << "\t"; 
2) std::cout << argv[i] << setw(15); 
3) std::cout << setw(15) << argv[i]; 

但它給我的格式不正確。

1 mark [email protected] 98989898 mark mark ADMN U Where are you studying SIM 0 
2 ken [email protected] 92222898 ken ken ADMN U Where are you studying SIM 0 
3 joseph [email protected] 92333718 joseph joseph ADMN U Where are you studying SIM 0 
4 ricky [email protected] 81822801 ricky ricky ADMN U Where are you studying SIM 0 
5 ck [email protected] 94567828 ck ck ADMN U Where are you studying SIM 0 

回答

0

請勿在for循環內使用std::endl。該操縱器導致for循環中的每個項目都被打印在一個新行中。

+0

是的,我確實刪除了std :: endl,並嘗試使用後三者之一: 1)std :: cout << argv [i] <<「\ t」; 2)std :: cout << argv [i] << setw(15); 3)std :: cout << setw(15)<< argv [i]; – 2013-02-17 03:13:29