#include <stdio.h>
#include <iostream>

#include <string.h>
#include <stdlib.h>
#include <math.h>
//#include "/usr/local/mysql/include/mysql/mysql.h"
#include "/usr/local/mysql/include/mysql.h"
using namespace std;
int main(void)
{
    cout << "Content-type: text/html \n\n";
    char mysqlServer[20] = "172.25.38.70";
    char query[300];
    MYSQL myData;
    MYSQL_RES *res;
    MYSQL_FIELD *fd;
    MYSQL_ROW row;
    int rowCount = 0;
    int colCount = 0;
    int i, j;

    mysql_init( &myData );
    if(!mysql_real_connect(&myData, mysqlServer, "root", "","test3",3306,NULL,0))  
    {
        printf("connect mysql error!\n");
        return 0;
    }
    mysql_query(&myData,"set names utf8");
    sprintf(query,"select * from test3");
    if( mysql_query(&myData, query) != 0 )
    {
        printf("query error!\n");
        return 0;
    }
    else
    {
        res = mysql_store_result( &myData );
        rowCount = (int) mysql_num_rows( res );
        colCount = (int) mysql_num_fields( res );

    printf("mysql result: %d records found\n fields: %d \n", rowCount, colCount);
    for(i = 0; i < rowCount; i++)
    {
      row = mysql_fetch_row( res );
      for( j = 0; j < colCount; j++)
      {
          printf("[ %s ] ", row[j] );
            printf(" \t");
      }
  printf("______ %d\n",i);
  printf(" \n ");
}
}

}

正确的编译命令代码:
g++ c_mysql.cpp -L/usr/local/mysql/lib/  -lmysqlclient -lz -lm -o cout

注意:
如果/tmp/ccTGmMS21.o: In function `main':
/tmp/ccTGmMS21.o(.text+0x11): undefined reference to `mysql_init'
那么参数增加-L/usr/lib/mysql -lmysqlclient  
如果
usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_uncompress':
my_compress.o(.text+0xaa): undefined reference to `uncompress'
那么增加-lz参数
规范点: g++ c_mysql.cpp -L/usr/local/mysql/lib/  -lmysqlclient -lz -lm
mail# cd /usr/ports/net/ntop
mail# make install clean
===>  Installing for ntop-3.3_1
===>   ntop-3.3_1 depends on executable: dot - not found
===>    Verifying install for dot in /usr/ports/graphics/graphviz
===>   graphviz-2.14.1_1 depends on executable: wish8.4 - found
===>   graphviz-2.14.1_1 depends on executable: gmake - found
===>   graphviz-2.14.1_1 depends on executable: bison - found
===>   graphviz-2.14.1_1 depends on file: /usr/local/bin/libtool - found
===>   graphviz-2.14.1_1 depends on file: /usr/local/libdata/xorg/libraries - not found
===>    Verifying install for /usr/local/libdata/xorg/libraries in /usr/ports/x11/xorg-libraries
/usr/X11R6 exists, but it is not a symlink. Installation cannot proceed.
This looks like an incompletely removed old version of X.  In the current version, /usr/X11R6 must be a symlink if it exists at all.Please read /usr/ports/UPDATING (entry of 20070519) for the procedure to upgrade X.org related ports.*** Error code 1

Stop in /usr/ports/x11/xorg-libraries.
*** Error code 1

Stop in /usr/ports/graphics/graphviz.
*** Error code 1

Stop in /usr/ports/net/ntop.
*** Error code 1

Stop in /usr/ports/net/ntop.
mail# rm /usr/X11R6/



*********
from the error messages:
usr/X11R6 exists, but it is not a symlink. Installation cannot proceed.
This looks like an incompletely removed old version of X.  In the current version, /usr/X11R6 must be a symlink if it exists at all.Please read /usr/ports/UPDATING (entry of 20070519) for the procedure to upgrade X.org related ports

SO WE SHOULD REMOVE THE sysmlink /usr/X11R6

>rm -r /usr/X11R6
#include <stdio.h>
#include <string>
#include <sstream>

using namespace std;

int main(void)
{
       string a = "312.29";
       string b;
       float f;
       stringstream mm;

       mm << a;
       mm >> f;
       printf("f=[%f]\n", f);

       mm.clear();
       mm << f;
       mm >> b;
       printf("b=[%s]\n", b.c_str());

       return 0;
}


#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;
int main(void)
{
       float f;
       string a = "312.29";
       char c[100];
       string b;

       f = atof(a.c_str());
       printf("f=[%f]\n", f);

       if (f>2008)
               printf("more than 2008\n");
       else
               printf("less than 2008\n");

       sprintf(c, "%f", f);
       printf("c=[%s]\n", c);

       b = c;
       printf("b=[%s]\n", b.c_str());

       return 0;
}
atoi,atol,strtod,strtol,strtoul实现类型转换
所属类别:C/C++
推荐指数:★★★☆
文档人气:427
本周人气:24
发布日期:2007-6-6
atof(将字符串转换成浮点型数)
相关函数
    atoi,atol,strtod,strtol,strtoul
表头文件
    #include <stdlib.h>
定义函数
    double atof(const char *nptr);
函数说明
    atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值
    返回转换后的浮点型数。
附加说明
    atof()与使用strtod(nptr,(char**)NULL)结果相同。
范例
    /* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char *a=”-100.23”;
char *b=”200e-2”;
float c;
c=atof(a)+atof(b);
printf(“c=%.2f ”,c);
}
执行
    c=-98.23
 


   
atoi(将字符串转换成整型数)
相关函数
    atof,atol,atrtod,strtol,strtoul
表头文件
    #include<stdlib.h>
定义函数
    int atoi(const char *nptr);
函数说明
    atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。
返回值
    返回转换后的整型数。
附加说明
    atoi()与使用strtol(nptr,(char**)NULL,10);结果相同。
范例
    /* 将字符串a 与字符串b转换成数字后相加*/
#include<stdlib.h>
mian()
{
char a[]=”-100”;
char b[]=”456”;
int c;
c=atoi(a)+atoi(b);
printf(c=%d ”,c);
}
执行
    c=356
 


   
atol(将字符串转换成长整型数)
相关函数
    atof,atoi,strtod,strtol,strtoul
表头文件
    #include<stdlib.h>
定义函数
    long atol(const char *nptr);
函数说明
    atol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('')才结束转换,并将结果返回。
返回值
    返回转换后的长整型数。
附加说明
    atol()与使用strtol(nptr,(char**)NULL,10);结果相同。
范例
    /*将字符串a与字符串b转换成数字后相加*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=” 234567890”;
long c;
c=atol(a)+atol(b);
printf(“c=%d ”,c);
}
执行
    c=1234567890
 


   
gcvt(将浮点型数转换为字符串,取四舍五入)
相关函数
    ecvt,fcvt,sprintf
表头文件
    #include<stdlib.h>
定义函数
    char *gcvt(double number,size_t ndigits,char *buf);
函数说明
    gcvt()用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。gcvt()与ecvt()和fcvt()不同的地方在于,gcvt()所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。
返回值
    返回一字符串指针,此地址即为buf指针。
附加说明
   
范例
    #include<stdlib.h>
main()
{
double a=123.45;
double b=-1234.56;
char *ptr;
int decpt,sign;
gcvt(a,5,ptr);
printf(“a value=%s ”,ptr);
ptr=gcvt(b,6,ptr);
printf(“b value=%s ”,ptr);
}
执行
    a value=123.45
b value=-1234.56
 


   
strtod(将字符串转换成浮点数)
相关函数
    atoi,atol,strtod,strtol,strtoul
表头文件
    #include<stdlib.h>
定义函数
    double strtod(const char *nptr,char **endptr);
函数说明
    strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。
返回值
    返回转换后的浮点型数。
附加说明
    参考atof()。
范例
    /*将字符串a,b,c 分别采用10,2,16 进制转换成数字*/
#include<stdlib.h>
mian()
{
char a[]=”1000000000”;
char b[]=”1000000000”;
char c[]=”ffff”;
printf(“a=%d ”,strtod(a,NULL,10));
printf(“b=%d ”,strtod(b,NULL,2));
printf(“c=%d ”,strtod(c,NULL,16));
}
执行
    a=1000000000
b=512
c=65535
 


   
strtol(将字符串转换成长整型数)
相关函数
    atof,atoi,atol,strtod,strtoul
表头文件
    #include<stdlib.h>
定义函数
    long int strtol(const char *nptr,char **endptr,int base);
函数说明
    strtol()会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如 base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用 16进制做转换。一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
返回值
    返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明
    ERANGE指定的转换字符串超出合法范围。
范例
    /* 将字符串a,b,c 分别采用10,2,16进制转换成数字*/
#include<stdlib.h>
main()
{
char a[]=”1000000000”;
char b[]=”1000000000”;
char c[]=”ffff”;
printf(“a=%d ”,strtol(a,NULL,10));
printf(“b=%d ”,strtol(b,NULL,2));
printf(“c=%d ”,strtol(c,NULL,16));
}
执行
    a=1000000000
b=512
c=65535
 


   
strtoul(将字符串转换成无符号长整型数)
相关函数
    atof,atoi,atol,strtod,strtol
表头文件
    #include<stdlib.h>
定义函数
    unsigned long int strtoul(const char *nptr,char **endptr,int base);
函数说明
    strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。
返回值
    返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。
附加说明
    ERANGE指定的转换字符串超出合法范围。
范例
    参考strtol()
 


   
toascii(将整型数转换成合法的ASCII 码字符)
相关函数
    isascii,toupper,tolower
表头文件
    #include<ctype.h>
定义函数
    int toascii(int c)
函数说明
    toascii()会将参数c转换成7位的unsigned char值,第八位则会被清除,此字符即会被转成ASCII码字符。
返回值
    将转换成功的ASCII码字符值返回。
范例
    #include<stdlib.h>
main()
{
int a=217;
char b;
printf(“before toascii () : a value =%d(%c) ”,a,a);
b=toascii(a);
printf(“after toascii() : a value =%d(%c) ”,b,b);
}
执行
    before toascii() : a value =217()
after toascii() : a value =89(Y)
 


   
tolower(将大写字母转换成小写字母)
相关函数
    isalpha,toupper
表头文件
    #include<stdlib.h>
定义函数
    int tolower(int c);
函数说明
    若参数c为大写字母则将该对应的小写字母返回。
返回值
    返回转换后的小写字母,若不须转换则将参数c值返回。
附加说明
   
范例
    /* 将s字符串内的大写字母转换成小写字母*/
#include<ctype.h>
main()
{
char s[]=”aBcDeFgH12345;!#$”;
int i;
printf(“before tolower() : %s ”,s);
for(i=0;I<sizeof(s);i++)
s=tolower(s);
printf(“after tolower() : %s ”,s);
}
执行
    before tolower() : aBcDeFgH12345;!#$
after tolower() : abcdefgh12345;!#$
 


   
toupper(将小写字母转换成大写字母)
相关函数
    isalpha,tolower
表头文件
    #include<ctype.h>
定义函数
    int toupper(int c);
函数说明
    若参数c为小写字母则将该对映的大写字母返回。
返回值
    返回转换后的大写字母,若不须转换则将参数c值返回。
附加说明
   
范例
    /* 将s字符串内的小写字母转换成大写字母*/
#include<ctype.h>
main()
{
char s[]=”aBcDeFgH12345;!#$”;
int i;
printf(“before toupper() : %s ”,s);
for(i=0;I<sizeof(s);i++)
s=toupper(s);
printf(“after toupper() : %s ”,s);
}
执行
    before toupper() : aBcDeFgH12345;!#$
after toupper() : ABCDEFGH12345;!#$


atof(将字符串转换成浮点型数)  
相关函数  atoi,atol,strtod,strtol,strtoul

表头文件  #include <stdlib.h>

定义函数  double atof(const char *nptr);

函数说明  atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。

返回值  返回转换后的浮点型数。

附加说明  atof()与使用strtod(nptr,(char**)NULL)结果相同。

atoi(将字符串转换成整型数)  
相关函数  atof,atol,atrtod,strtol,strtoul

表头文件  #include<stdlib.h>

定义函数  int atoi(const char *nptr);

函数说明  atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。

返回值  返回转换后的整型数。

附加说明  atoi()与使用strtol(nptr,(char**)NULL,10);结果相同。

atol(将字符串转换成长整型数)  
相关函数  atof,atoi,strtod,strtol,strtoul

表头文件  #include<stdlib.h>

定义函数  long atol(const char *nptr);

函数说明  atol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。

返回值  返回转换后的长整型数。

附加说明  atol()与使用strtol(nptr,(char**)NULL,10);结果相同。

gcvt(将浮点型数转换为字符串,取四舍五入)  
相关函数  ecvt,fcvt,sprintf

表头文件  #include<stdlib.h>

定义函数  char *gcvt(double number,size_t ndigits,char *buf);

函数说明  gcvt()用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。gcvt()与ecvt()和fcvt()不同的地方在于,gcvt()所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。

返回值  返回一字符串指针,此地址即为buf指针。

strtod(将字符串转换成浮点数)  
相关函数  atoi,atol,strtod,strtol,strtoul

表头文件  #include<stdlib.h>

定义函数  double strtod(const char *nptr,char **endptr);

函数说明  strtod()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,到出现非数字或字符串结束时('\0')才结束转换,并将结果返回。若endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr传回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分。如123.456或123e-2。

返回值  返回转换后的浮点型数。

strtol(将字符串转换成长整型数)  
相关函数  atof,atoi,atol,strtod,strtoul

表头文件  #include<stdlib.h>

定义函数  long int strtol(const char *nptr,char **endptr,int base);

函数说明  strtol()会将参数nptr字符串根据参数base来转换成长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。

返回值  返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。

附加说明  ERANGE指定的转换字符串超出合法范围。

strtoul(将字符串转换成无符号长整型数)  
相关函数  atof,atoi,atol,strtod,strtol

表头文件  #include<stdlib.h>

定义函数  unsigned long int strtoul(const char *nptr,char **endptr,int base);

函数说明  strtoul()会将参数nptr字符串根据参数base来转换成无符号的长整型数。参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制数等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换。一开始strtoul()会扫描参数nptr字符串,跳过前面的空格字符串,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回。

返回值  返回转换后的长整型数,否则返回ERANGE并将错误代码存入errno中。

附加说明  ERANGE指定的转换字符串超出合法范围。

toascii(将整型数转换成合法的ASCII 码字符)  
相关函数  isascii,toupper,tolower

表头文件  #include<ctype.h>

定义函数  int toascii(int c)

函数说明  toascii()会将参数c转换成7位的unsigned char值,第八位则会被清除,此字符即会被转成ASCII码字符。

返回值  将转换成功的ASCII码字符值返回。

老版本了,新版本看,phpMyAdmin正确的安装配置:http://www.cnblogs.com/vit4/archive/2012/10/26/2741291.html

注意是不是PHP的session没有打开,打开办法:https://jackxiang.com/post/8379/
————————————————————————————————————————

1、先下载 phpMyAdmin 安装包 ,http://www.phpmyadmin.net

2、解压后一个单独目录中(你可以自定义目录名称)

3、找到 /libraries/config.default.php文件(旧版本是根目录下的config.inc.php文件),用写字板(不要用记事本,这是UTF8编码)进行编辑。

4、查找 $cfg['PmaAbsoluteUri']
修改为你将上传到空间的phpMyAdmin的网址
如:$cfg['PmaAbsoluteUri'] = 'http://bbs.bitscn.com/phpmyadmin/';

5、查找 $cfg['Servers'][$i]['host'] = 'localhost';(通常用默认,也有例外,可以不用修改)

6、查找 $cfg['Servers'][$i]['auth_type'] = 'config'; (本人改为cookie后出现问题,最好别改)
在自己的机子里调试用config;如果在网络上的空间用cookie,这里我们既然在前面已经添加了网址,就修改成cookie ,这里建议使用cookie.

7、查找 $cfg['Servers'][$i]['user'] = 'root'; // MySQL user(用户名,自己机里用root;在网上一般为你的ftp用户名,虚拟主机提供商会告诉你的;一般不要修改)

8、查找 $cfg['Servers'][$i]['password'] = ''; // MySQL password (only needed
自己机里不用设,留空就可以了

9、查找 $cfg['Servers'][$i]['only_db'] = ''; // If set to a db-name, only(你只有一个数据就设置一下;如果你在本机或想架设服务器,那么建议留空)

10、查找 $cfg['DefaultLang'] = 'zh'; (这里是选择语言,zh代表简体中文的意思)

安装完保存OK

登陆时出现:配置文件现在需要绝密的短语密码(blowfish_secret)  

config.default.php文件里面进行设置
$cfg['blowfish_secret'] = 'cookie';
$cfg['Servers'][$i]['auth_type']     = 'cookie';

如果是在配置正确的情况下清空cookie就可以了。

http_post.h
[codes=php]
#ifndef __HTTP_POST__
#define __HTTP_POST__

#define SERVER_ADDR "123.57.252.183"
#define SERVER_PORT 80
#define SERVER_URL  "ai.egg.levoo.com"
#define SERVER_PATH "/Api/upload"

#define HTTP_HEAD   "POST %s HTTP/1.1\r\n"\
                    "Host: %s\r\n"\
                    "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:59.0) Gecko/20100101 Firefox/59.0\r\n"\
                    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"\
                    "Accept-Language: en-US,en;q=0.5\r\n"\
                    "Accept-Encoding: gzip, deflate\r\n"\
                    "Content-Type: multipart/form-data; boundary=%s\r\n"\
                    "Content-Length: %ld\r\n"\
                    "Connection: close\r\n"\
                    "Upgrade-Insecure-Requests: 1\r\n"\
                    "DNT: 1\r\n\r\n"\


#define UPLOAD_REQUEST  "--%s\r\n"\
                        "Content-Disposition: form-data; name=\"image\"; filename=\"%s\"\r\n"\
                        "Content-Type: image/jpeg\r\n\r\n"

unsigned long get_file_size(const char *path);

int http_post_upload_pic(const unsigned char *IP, const unsigned int port,char *URL, const char *filepath,
                                    char *ack_json, int ack_len); //Post方式上传图片

#endif
[/codes]

[codes=php]
#cat snprint.c
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/stat.h>
#include "http_post.h"

unsigned char http_boundary[64]={0};
unsigned char send_request[1024]={0};
unsigned char send_end[1024]={0};
int main(int argc, char *argv[])
{
    long long int timestamp;
    struct timeval tv;
    timestamp = (long long int)tv.tv_sec * 1000 + tv.tv_usec;
    snprintf(http_boundary,64,"---------------------------%lld",timestamp);
    const char *filepath = argv[1];
    unsigned long totalsize = 0;
    unsigned long filesize = -1;
    unsigned long request_len = snprintf(send_request,1024,UPLOAD_REQUEST,http_boundary,filepath); //请求信息
    unsigned long end_len = snprintf(send_end,1024,"\r\n--%s--\r\n",http_boundary); //结束信息
    struct stat statbuff;
    if(stat(filepath, &statbuff) < 0){
        return filesize;
    }else{
        filesize = statbuff.st_size;
    }
    printf("eggpic.jpeg's filesize %ld\n",filesize);
    return 0;
}
[/codes]
#gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /data/codesdev/http_post/a.out...done.
(gdb) set args eggpic.jpeg
(gdb) b 23
Breakpoint 1 at 0x400645: file snprint.c, line 23.
(gdb) r
Starting program: /data/codesdev/http_post/a.out eggpic.jpeg

Breakpoint 1, main (argc=2, argv=0x7fffffffe798) at snprint.c:23
23          unsigned long end_len = snprintf(send_end,1024,"\r\n--%s--\r\n",http_boundary); //结束信息
(gdb) p argv[1]
$1 = 0x7fffffffea21 "eggpic.jpeg"

(gdb) p filepath
$2 = 0x7fffffffea1b "eggpic.jpeg"

if(stat(filepath, &statbuff) < 0){
(gdb) p send_request
$4 = '-' <repeats 29 times>, "4197109\r\nContent-Disposition: form-data; name=\"image\"; filename=\"eggpic.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n", '\000' <repeats 887 times>

30          printf("eggpic.jpeg's filesize %ld\n",filesize);
(gdb) p filesize
$3 = 13473
(gdb) n
eggpic.jpeg's filesize 13473
31          return 0;
(gdb) n

snprintf在C语言里字符串上,Http里使用较多,
int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...);
函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0。所以如果目标串的大小为n
                 的话,将不会溢出。

函数返回值:若成功则返回存入数组的字符数,若编码出错则返回负值。

Result1(推荐的用法)

       #include <stdio.h>
       #include <stdlib.h>
      
       int main()
       {
      
           char str[10];
           snprintf(str,sizeof(str),"0123456789012345678");
           printf("str = %s \n",str);
          return 0;
     }

root@darkstar:/home/zhangl/unixtest/chapter9# ./testsprintf    

str = 012345678


Result2:(不推荐使用)

        #include <stdio.h>
       #include <stdlib.h>
      
       int main()
       {
      
           char str[10];
           snprintf(str,18,"0123456789012345678");
           printf("str = %s \n",str);
          return 0;
    }

root@darkstar:/home/zhangl/unixtest/chapter9# ./testsprintf                    

str = 01234567890123456


snprintf函数返回值的测试:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
           char str[10];
           int n=0;
    
           n=snprintf(str,sizeof(str),"%s","abc");
              printf("str = %s \n",str);
              printf("n=%d\n",n);
      
              return 0;
   }


Result:

root@darkstar:/home/zhangl/test# ./testsnprintf    
str = abc
n=3
select left(email,locate('@',email)-1),(select name from domain where domain.enterpriseid = email.enterpriseid limit 1) as name from email where id=492699 and left(email,locate('@',email)-1)='chengjr' limit 1
如有困难参考:

http://www.toplee.com/blog/329.html
http://tieba.baidu.com/f?kz=206696384
http://oss.lzu.edu.cn/blog/article.php?tid_45.html


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>  
#include "/usr/local/mysql/include/mysql/mysql.h"
int main(void)
{
char mysqlServer[20] = "10.88.15.114";
char query[300];
MYSQL myData;
MYSQL_RES *res;
MYSQL_FIELD *fd;
MYSQL_ROW row;
int rowCount = 0;
int colCount = 0;
int i, j;

mysql_init( &myData );
if(mysql_real_connect( &myData, mysqlServer, "web", "sinatest", "enterprise",3306,NULL,0))
{
printf("connect mysql error!\n");
return 0;
}

sprintf(query,"select * from domain");
if( mysql_query(&myData, query) != 0 )
{
printf("query error!\n");
return 0;
}
else
{
res = mysql_store_result( &myData );
rowCount = (int) mysql_num_rows( res );
colCount = (int) mysql_num_fields( res );
printf(" result: %d records found\n fields: %d \n", rowCount, colCount);
row = mysql_fetch_row( res );
for(i = 0; i < rowCount; i++)
{
printf(" show: ");
for( j = 0; j < colCount; j++)
{
printf("[ %s ] ", row[j] );
} // end for
printf(" \n ");
} // end for
}

return 0;
}


--------------------------------------------------------------
编译:
gcc -o aaa mysqlconn.c -L /usr/local/mysql/lib/mysql/*.a -lz -lm(混乱哈哈)
注意:
要#include <math.h> 编译的时候要加上-lm.
否则出现:

/usr/local/mysql/lib/mysql/libmysqlclient.a(password.o)(.text+0x308): In function `scramble_323':
: undefined reference to `floor'


连接是这样做的:  

if(mysql_real_connect(conn,host_name,user_name,password,port_num,socket_name,flags)==null)  
  {  
  print_error(conn,"mysql_real_connect()   failed");  
  return(null);  
  }  
  if(db_name   !=null)  
  {  
    if(mysql_select_db(conn,db_name)!=0)\{  
    {  
        print_error(conn,"mysql_select_db()   failed");  
#file a b c makefile
CGI_DIR =bin


all:myfile

myfile:filea.o fileb.o filec.o
        gcc filea.o fileb.o filec.o -o myfile
filea.o:filea.c head.h
        gcc -c filea.c
fileb.o:fileb.c head.h
        gcc -c fileb.c
filec.o:filec.c head.h
        gcc -c filec.c

install:
        @if [ ! -d $(CGI_DIR) ]; then \
        mkdir -p $(CGI_DIR); \
        fi
        cp myfile /usr/home/xiangdong2/c++/make/$(CGI_DIR)




clean:
        rm -f *.o;
        rm -rf myfile
        rm -rf bin

(注意:这儿的要执行的命令要用tab键隔开,否则出现:operation erro)



———————————shell环境下直接make生成可供gdb调试的二进制文件。—————————————
root@192.168.137.128:~/dev_codes_all/arts_debug_book/arts_debug/chapt1# make ins CFLAGS="-Wall -o2 -g"
cc -Wall -o2 -g    ins.c   -o ins
root@192.168.137.128:~/dev_codes_all/arts_debug_book/arts_debug/chapt1# gdb ins
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/dev_codes_all/arts_debug_book/arts_debug/chapt1/ins...done.
(gdb) l
53      void print_results()
54      {
55        int i;
56        for (i = 0; i < num_inputs; i++)
57          printf("%d\n", y[i]);
58      }
59          
60      int main(int argc,char **argv)
61      {
62        get_args(argc,argv);
(gdb)
“我将给他一个无法拒绝的理由……”恐怕世界上在没有哪个演员能够演绎《教父》中,马龙·白兰度的这句对白,“教父”在人们心中所留下难以泯灭的印象是男人、是男人的家庭和男人的爱。
“不要憎恨你的敌人,否则你将做出错误的判断。”
,“让朋友低估你的优点,让敌人高估你的缺点……”
不照顾家人的男人,根本算不上是个男人!”

http://forum.ubuntu.org.cn/about63843-0-asc-0.html



[url=http://www.ithaka.cn/Down/View.asp?id=11][/url]
cd /usr/ports/editors/vim6
make
make install
内容:
系统标识
时间与日期
多进程编程


1. 系统标识符
   a.获得有关的系统信息
      #include
      int uname( struct utsname * name);// 用 man uname 查看 struct utsname
   b.获得系统的名称
      #include
      int gethostname( char* name, int namelen ); // 成功返回 0, 否则返回 1
      例子:
     

      #include<iostream>
 #include<unistd.h>
 #include<sys/utsname.h>
 
 using namespace std;
 
 int main(){
  cout<<"-------------- hostname ------------"<<endl;
  char name[ 100 ];
  memset( name, 0x00, sizeof( name ) );
  gethostname( name, sizeof( name ) );
  cout<< name <<endl;
 
  cout<<"-------------- uname ----------------"<<endl;
  struct utsname nm;
  memset( &nm, 0x00, sizeof( nm ) );
  uname( &nm );
  cout<<"sysname ="<< nm.sysname <<endl;
  cout<<"nodename ="<< nm.nodename <<endl;
  cout<<"release ="<< nm.release <<endl;
  cout<<"version ="<< nm.version <<endl;
  cout<<"machine ="<< nm.machine <<endl;
  return 0;
 }
 

 
2. 时间与日期
   4种表示形式:(1)time_t 1970年至今的秒数               (2) struct tm 以结构表示
                         (3) char * 字符串                               (4) formated char*  自定义格式
   (1)-->(2) 转换有函数: localtime() // 本地时间            gmtime() // 格林威治时间
   (2)-->(1) 转换有函数: mktime()
   (1)-->(3) 转换有函数: ctime()
   (2)-->(3) 转换有函数: asctime()
   (2)-->(4) 转换有函数: strftime()
   例子:
   

   #include<iostream>
#include<time.h>
using namespace std;

int main(){
 // time() get kernel time
 cout<<"-----------time_t-----------"<<endl;
 time_t t=0;
 time( &t );
 cout<< t <<endl;
 
 // time_t --> struct tm, localtime(),gmtime()
 // man localtime to see the struct tm
 cout<<"-----------struct tm-----------"<<endl;
 struct tm *p=NULL;
 p=localtime( &t );  
 cout<<"year:"<< p->tm_year+1900 <<endl; // the number of years since 1900
 cout<<"month:"<< p->tm_mon+1 <<endl; // tm_mon ranges 0 to 11
 cout<<"day:"<< p->tm_mday <<endl;
 
 // struct tm --> time_t
 cout<<"-----------mktime-----------"<<endl;
 time_t t1;
 t1=mktime( p );
 cout<< t1 <<endl;
 
 // time_t --> char *
 cout<<"-----------ctime-----------"<<endl;
 cout<< ctime( &t ) <<endl;
 char ct[ 100 ];                        //如果想保持返回值应开辟新空间,不能直接定义指针保存
 memset( ct, 0x00, sizeof( ct ) ); //因为ctime的返回值总是在同一空间,下次调用ctime时就会被更改
 strcpy( ct, ctime( &t ) );
 cout<< ct <<endl;
 
 // struct tm --> char *
 cout<<"-----------asctime-----------"<<endl;
 cout<< asctime( p ) <<endl;
 
 // struct tm --> formated char*
 cout<<"-----------strftime-----------"<<endl;
 char ft[ 100 ];
 memset( ft, 0x00, sizeof( ft ) );
 strftime( ft, sizeof( ft ), "%Y-%m-%d %H:%M:%S", p );
 cout<< ft <<endl;
 return 0;
}


3.system 函数
  #include
  int system( const char * string );
  执行string 所表示的命令,将产生一个新的进程,system为阻塞函数, 新的进程结束后才继续
  例子:
 

  #include<iostream>
#include<stdlib.h>
using namespace std;

int main(){
 cout<<"----------begin---------"<<endl;
 system("ls -l");
 cout<<"----------end-----------"<<endl;
 return 0;
}

// 一个简单的shell
#include<iostream>
#include<unistd.h>
using namespace std;

int main(){
 char cmd[ 100 ];
 memset( cmd, 0x00, sizeof( cmd ) );
 while( 1 ){
  cout<<"[irini@localhost]#";
  cin.getline( cmd,sizeof( cmd ) );
  if( strcmp( cmd,"bye" )==0 ) break;
  system( cmd );
 }
 return 0;
}



4. atexit() 函数
  #include
  int atexit( void (*func) (void) );
  登记exit handler,最多可登记32个,在进程退出时最后登记的先调用,最先登记的最后调用
 
5. exit 与 _exit
   * 进程的退出过程:
   进程做的事: exit handler( atexit注册的), 关闭IO流,如果申请了堆空间就释放
   ------------------------------------------------------------------
   kernel做的事: 销毁进程空间, 删除进程表中的相应项
   *  exit 是正常退出,想做进程的,然后进入kernel处理
      _exit 是异常退出,直接进入kernel
   
   例子:
   

   #include<iostream>
#include<unistd.h>
using namespace std;

void fn1(){
 cout<<"in fn1()..."<<endl;
}
void fn2(){
 cout<<"in fn2()..."<<endl;
}
int main(){
 atexit( fn1 );
 atexit( fn2 );
 cout<<"return from main..."<<endl;
 //exit( 0 );
 _exit( 0 );
}



6. 进程标识符
  #include
  #include
  pid_t getpid(); // 当前进程号
  pid_t getppid(); // 得到父进程号
  例子:
 

  #include<iostream>
#include<sys/types.h>
#include<unistd.h>
using namespace std;

int main(){
 cout<<"pid="<<getpid()<<endl;
 cout<<"ppid="<<getppid()<<endl;
 return 0;
}



7. fork 函数
   * 创建一个新进程,这个进程是父进程的完全拷贝,完全拷贝父进程的进程空间,唯一的区别是fork()的返回值不同
     返回给父进程的是子进程的pid, 返回给子进程的是0        
     
     例子:
     

     #include<iostream>
 #include<sys/types.h>
 #include<unistd.h>
 using namespace std;
 
 int main(){
  pid_t cid=fork();
  if( cid==0 ){
   for( int i=0; i<5; i++ ){
    cout<<"[child] pid="<<getpid()<<" ppid="<<getppid()<<endl;
    sleep( 1 );
   }
   exit( 0 );
  }else if( cid > 0 ){
   for( int i=0; i<5; i++ ){
    cout<<"[father] pid="<<getpid()<<" cid="<<cid<<endl;
    sleep( 1 );
   }
   exit( 0 );
  }else{
   cout<<"error"<<endl;
   exit( -1 );
  }
  return 0;
 }
 

 
  * fork后,父子进程相互独立,如果之前父进程申请了一个堆空间,那之后父子进程中的指针值相同吗?
    通过程序可验证他们是相同的,但指向的空间是不同,因为进程中分配给指针的不是绝对地址,
    是逻辑偏移地址,进程空间中正文段的起始地址是逻辑0
   
  * 如果父进程在子进程之前退出,子进程会被初始化进程(pid=1) 托管
    如果子进程在父进程之前退出,子进程不会被销毁,变为僵死进程 Z状态,等待父进程处理
   
    例子:
   

    #include<iostream>
 #include<sys/types.h>
 #include<unistd.h>
 using namespace std;
 
 void fn(){
  cout<<"in fn()... "<<endl;
 }
 int main(){
  atexit( fn );
  pid_t cid=fork();
  if( cid==0 ){
   for( int i=0; i<5; i++ ){
    cout<<"[child] pid="<<getpid()<<endl;
    sleep( 1 );
   }
   exit( 0 );
  }else if( cid > 0 ){
   cout<<"father exit..."<<endl;
   exit( 0 );
  }else{
   cout<<"error"<<endl;
   exit( -1 );
  }
  return 0;
 }
 

 
8. wait 和 waitpid 函数
   处理结束的子进程,是阻塞函数
   #include
   #include
   pid_t wait( int * statloc );
   pid_t waitpid( pid_t pid, int *static, int option );
   返回值为子进程的pid
   statloc 用于接受终止的子进程的返回状态
   option 通常设为0  
   
   
   例子:
   

   #include<iostream>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
using namespace std;

int main(){
 pid_t cid=fork();
 if( cid==0 ){
  sleep( 3 );
  cout<<"[child] pid="<<getpid()<<" ppid="<<getppid()<<endl;
  exit( 0 );
 }else if( cid > 0 ){
  cout<<"[father] pid="<<getpid()<<" cid="<<cid<<endl;
  int statloc=0;
  pid_t id=waitpid( cid, &statloc, 0 );
  cout<<"[child] "<< id <<" exited"<<endl;
  cout<<"exit value is "<< statloc <<endl;
  if( WIFEXITED( statloc ) )
   cout<<"value "<<WEXITSTATUS( statloc )<<endl;
  else  cout<<"signal "<<WTERMSIG( statloc )<<endl;
 }else{
  cout<<"error"<<endl;
  exit( -1 );
 }
 return 0;
}


9. exec 函数
  类似system,建立新的进程,但不新建进程空间,而是把原进程空间清0变成自己的开始执行
  则原进程exec之后的代码不在存在,失效
 
  例子:
  [code]
  #include
#include
using namespace std;

int main(){
 cout<<"-------------begin-----------"<  //execlp( "ls","ls","-l","-a",NULL );   // 第一个参数为要执行的命令文件名,后面的为命令行参数(从0开始都要写,以NULL结束)
 char* argv[]={ "ls","-l","-a",NULL };
 execvp( "ls", argv );
 cout<<"-------------end-------------"<  return 0;
}
1 安装系统和软件;

通过安装,我们能了解Linux的目录结构;系统和软件的安装方法,以及基本目录、文件和用的操作,没有比这些更基础的吧。

2 对硬件的安装和维护;

Linux是个系统,我们得把她用起来才能达到我们的目的。在生产、生活和或者娱乐中的应用,最能体现她的价值。比如我们要让Linux支持 scsi ;raid ;usb; firewire; mouse; video card;TV card 等,无非就是应用。比如我有鼠标,在我的Linux中却用不起来,是不是有点浪费??

3 用户管理;

Linux是一个多用户,多任务的系统,要让很多人能同时用这台机器的Linux,我们不得不经常对用户进行增加或者删除。有的弟兄可能会说,我的机器上只有一个显示器一套键盘和鼠标,怎么让更多的用户应用呢??可能初学Linux的弟兄可能早就明白了,比如 web服务器,是不是多用户的呢??ftp服务器也应该是多用户的吧。咱们不是有远程登录ssh 和telnet吗?这个多用户可不是一台机器,几个人同时挤在同一个键盘和显示器上用不同用户名登录系统。

4 磁盘管理;

磁盘是有限的,就是再大的磁盘需要管理。因为我们都是玩家,再大的磁盘也感觉小。我现在有160的磁盘,我感觉还是不够用,所以要把有限的空间都利用上,还得对磁盘有个计划。。比如限制用户家目录空间;限制用户上传文件大小;定时查看磁盘分区的利用率 。管理员经常用 fdisk -l 或者 df -h du -h 查来看分区,目录的大小等

5 检测系统状态;

有时学习Linux的弟兄总是问“为什么我的机器开机这么慢?”;“Linux真的是浪费内存,对不对?”等。其实这些问题都涉及到系统监测。比如 CPU、内存、网络利用率等。因为生产型系统是必须有效率的。如果一台服务器反应迟钝,可能最急的是管理员。管理员首先要做的可能是查看系统运行状态。比如用top ; sar ; netstat等 ;

6 安全和备份;

对于生产型的系统,没有比安全更重要了。如果发现安问题,可能管理员首要做的就是停止相应的服务,查看日志,执行备份,以及打补丁。

世上没有绝对安全的东西,硬件的可靠性没有百分之分的,Linux系统也不是百分之百的安全。每天进行日常备份还是极为必要的。比如我们把重要的数据用备份到一个较为安全的地方,比如磁带机,本地机,或者网络上的计算机上。

7 灾难恢复;

有了备份,才能有恢复之说,如果没有备份,那就只能是一切从头开始了。我想大家在玩自己的机器时也有所体会吧。灾难只能说是天灾了,真的遇到了灾难,咱们不能怨天忧人了。骂什么或者怪自己太大意都没有用,我们做的只能是自己承受,能恢复多少算多少吧。只要每天都用心尽力了,恢复还不是太大的问题。

8 网络管理;

Internet 是最流行的,自从我们在学校的BBS和恐龙MM聊天开始,可能我们就懂得了Internet的强大,虽然在学校可能是校园网,但我们还是感觉到了网的神秘。在电视中,我们经常看到七八十岁的老爷爷和老奶奶运指如飞,对着显示器聊的不亦乐乎。这就是net的魅力。我们可以在LinuxSir上聊天灌水,也是net的魅力。在LinuxSir的后面呢???这就是我们所要谈到的,管理和版所要做的,比如web服务器架设,FTP 、 IRC 、防火墙的架设;网络基本操作。比如简的设置IP,IP追踪; whois 这个ip是从如来的,各种网络服务器进程查看等 。

9 系统管理与日志分析;

这个概念有些大,有的人也把系统管理看上最高点,把用户管理;磁盘管理;网络管理;安全都纳入这个关健词之中。这也是有道理的。因为这么多的管理都是密不可分的,离了哪个都不行。当然还有好多的零活让我们来做,比如系统优化,内核编译等。系统管理概念比较大,麻烦事也比较多。以后在各部份一步一步的让初学Linux的弟兄体验体验,可能有的弟兄早就体验过了,只是不知道他就是在体验系统管理 。

日志分析对于我们了解系统运行还是极有帮助的,否则有人攻击我们的机器都不知道,那不麻烦了?通过分析日志,我们能得到硬件及各种软件的运行状态,以及他们配合的是否正常等。系统中的每个服务都有日志,这对于我们找出系统运行中出现故障有极大的帮助。有时发现问题比解决问题更难,我相信初学 Linux的弟兄慢慢就明白这个道理了。

10 开发:

玩Linux的最高境界,可能就是开发了。现在我还是处于对系统的学习阶段,所以不能写出什么开发的经验之谈。一想到自己能造出一个发行版自己用用,也是件高兴的事。

加油吧。。。。。。弟兄们!
分页: 256/271 第一页 上页 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 下页 最后页 [ 显示模式: 摘要 | 列表 ]