这三个月以来一直忙着赶进度,没有停下来记录一些东西,很多很好的东西往往只能零零散散地记在草稿本上, 这样偶尔想起来自己都找不到,所以现在抽空总结下来。

这些天做了三件事,其一是在Linux下开发了对接service的IPTV client lib on PC,之所以是on PC,因为我写的这段程序只是为了以后移植到一个运行RTOS的机顶盒上面;其二是基于PayPal做的支付系统,其三是一个监控服务器和用户状态的简单后台管理页面,这两个都是用cakephp + bootstrap做的,并没有涉及到数据库,与数据库交互是使用了已经写好的API。这篇记录C的内容。

一、打印指针值:

int a = ;
int *p = &a;
printf("%p", p);

二、

PHPStorm: PHP IDE

PyCharm: PYTHON IDE

Brackets : 强大免费的开源跨平台Web前端开发工具IDE

coolshell.com : 酷壳网

三、

问题:VMWare Workstation虚拟机出现故障,非正常关机时,无法再次打开。

解决:删除虚拟磁盘目录下的*.lck文件(可能需要重启)。

四、字符串的正确使用:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(int argc, char *argv[])

{

    char str1[] = "abcdefghijklmnopqrstuvwxyz";//编译器将在末尾自动添加“\0”,但是需要保证,字符数组中有空余的位置

    char *str2 = NULL;

    str2 = (char *)malloc( + );

    memset(str2, ,  + );

    memcpy(str2, str1, );

    printf("str1 length is %ld, size is %ld\n", strlen(str1), sizeof(str1));

    printf("str2 length is %ld, size is %ld\n", strlen(str2), sizeof(str2));

    int i = ;

    for(i = ; i < ; i++)

        printf("%02x ", str1[i]);

    printf("\n");

    for(i = ; i < ; i++)

        printf(" %c ", str1[i]);

    printf("\n");

    if(NULL != str2) free(str2);

    return ;

}

输出:

hubery@hubery-VirtualBox:~/CP/HW$ ./shellInput
str1 length is , size is
str2 length is , size is
6a 6b 6c 6d 6e 6f 7a
a b c d e f g h i j k l m n o p q r s t u v w x y z

尽量不要使用strlen、strcpy、strcat这些需要通过结束符“\0”来判断字符串长度的函数,因为一旦字符串没有正常的结束符,你将得到意想不到的结果。

五、在子函数中malloc变量(声明时字符串大小不可知):

#include <stdlib.h>
#include <stdio.h> int malloc_inner(char **p)
{
*p = (char *)malloc();
return ;
} int main()
{
char *str = null;
malloc_inner(&str);
free(str);
return ;
}

六、命令行参数:

//shellInput.c
#include <stdio.h>
#include <stdlib.h> int main(int argc, char *argv[])
{
printf("argc = %d, argv[0] = %s, argv[1] = %s\n", argc, argv[], argv[]);
return ;
} //output
#./shellInput hello
argc = , argv[] = ./shellInput, argv[] = hello

七、使用CGDB调试一个程序:

objdump,cgdb,dmesg -c.

编译参数: -g -rdynamic

调试: cgdb ./a.out

加断点: b func_name

步进: n

步入: s

打印: p

GDB/CGDB 调试时打印完整内容:set print element 0

查看dumped的位置: bt/where

使down一个程序: kill -s SIGSEGV pid

八、C读写文件:

snprintf(filename, sizeof(filename), "%s.sig", arv[]);
//write
if((f = fopen(filename, "wb+")) == NULL)
{
ret = ;
printf("...");
//...
}
if(fwrite(buf, , olen ,f) != olen)
{
printf("...");
//...
}
fclose(f); //read
f = fopen(filename,"rb");
i = fread(buf, , sizeof(buf), f);
fclose(f);

九、结构体

typedef struct {
slist_node node;
VdsChannel channel;
CHAR *type;
CHAR *video_id;
CHAR *url;//需要url时,先查询链表,若没有再作请求
}VdsChanInfo; struct timerr
{
time_t time_begin;
unsigned int time_long;
unsigned int time_refresh;
char user_id[];
char token[];
};
struct timerr timer ;//用法1 //结构体数组
struct
{
UINT32 isValid;//1 for true, 0 for false
UINT32 decode_type;//0 for all, 1 for sha1+rsa, 2 for aes+rsa
CHAR *request_encrypt_key;
CHAR *response_encrypt_key; }keyPairs[KEY_PAIR_SUM]; //构造枚举类型
typedef enum {
VDS_READY,
VDS_AUTH_OK,
VDS_GET_KEY_OK,
VDS_GET_CHAN_INIT_OK,
VDS_GET_CHAN_URL_OK
}VdsStatus; typedef struct { BOOL running;
BOOL exit;
VdsStatus status; VdsIptvParam param;
pthread_t thread_id;
pthread_mutex_t lock;
pthread_mutex_t debug_lock;
UINT32 chan_num;
slist channel_list; }VdsIptvMgr; VdsIptvMgr * get_mgr(void);//用法2

十、链表使用

 //singly_linked_list.h
#ifndef __SINGLY_LINKED_LIST_H__
#define __SINGLY_LINKED_LIST_H__
#include "porting.h" #ifdef __cplusplus
extern "C" {
#endif typedef struct _slist_node {
struct _slist_node *next;
} slist_node; typedef struct {
UINT32 count;
slist_node *head;
slist_node *tail;
} slist; static __inline__ void slist_add_head(slist *list, slist_node *node) {
if (list->count == ) {
list->head = node;
list->tail = node;
}
else {
node->next = list->head;
list->head = node;
}
list->count++;
} static __inline__ void slist_add_tail(slist *list, slist_node *node)
{
if (list->count == ) {
list->head = node;
list->tail = node;
}
else {
list->tail->next = node;
list->tail = node;
}
list->count++;
} static __inline__ slist_node * slist_del_head(slist *list)
{
slist_node *node = NULL;
if (list->count > ) {
node = list->head;
list->head = node->next;
node->next = NULL;
if (--list->count == ) {
list->tail = NULL;
}
}
return node;
} static __inline__ slist_node * slist_del_tail(slist *list) {
slist_node *node = NULL;
if (list->count > ) {
slist_node *ptr = list->head;
while (ptr) {
if (ptr->next == list->tail) {
node = list->tail;
ptr->next = NULL;
list->tail = ptr;
list->count--;
break;
}
ptr = ptr->next;
}
}
else if (list->count == ) {
node = list->tail;
list->head = list->tail = NULL;
list->count--;
} return node;
} static __inline__ slist_node * slist_del_node(slist *list, slist_node *node)
{
slist_node *curr = NULL;
slist_node *temp = NULL; if(list->head == node)
return slist_del_head(list); if(list->tail == node)
return slist_del_tail(list); //if(list->count <= 2)
//{
//ASSERT(0);
//} curr = list->head;
if(curr == NULL)
return NULL; while(curr->next != NULL)
{
if(curr->next == node)
{
temp = curr->next;
curr->next = temp->next;
list->count--; temp->next = NULL; /*
if(list->count >= 1)
{
if(list->head == NULL || list->tail == NULL)
{
SDBBP();
} if(list->count == 1 && list->head != list->tail)
{
SDBBP();
} if(list->count == 2 && list->head->next != list->tail)
{
SDBBP();
} if(list->tail->next != NULL)
{
SDBBP();
}
}
*/ return temp;
}
else
curr = curr->next;
} /*
if(list->count > 0)
{
if(list->tail->next != NULL)
{
SDBBP();
}
}
*/
return NULL;
} static __inline__ void slist_free(slist *list)
{
slist_node *node = NULL;
if(NULL == list)
return;
while(list->count > )
{
node = slist_del_head(list);
FREE(node);
}
} #define SLIST_COUNT(slist) ((slist)->count) /**
* SLIST_ENTRY - get the struct for this entry
* @ptr: the struct slist_node pointer.
* @type: the type of the struct @ptr embedded in.
* @member: the name of the slist_node within the struct.
*/
#define SLIST_ENTRY(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *))->member))) #ifdef __cplusplus
}
#endif /* __cplusplus */ #endif

singly_linked_list.h

下面展示如何使用以上链表操作。

//
typedef struct {
UINT32 idx;
CHAR *name;
CHAR *img_h;
CHAR *img_s;
}VdsChannel; typedef struct {
slist_node node;
VdsChannel channel;
CHAR *type;
CHAR *video_id;
CHAR *url;//需要url时,先查询链表,若没有再作请求
}VdsChanInfo; //declare
vds_list = (slist *)malloc(sizeof(slist));
vds_list->count = ;
vds_list->head = NULL;
vds_list->tail = NULL; //add
VdsChanInfo *channels = (VdsChanInfo *)malloc(sizeof(VdsChanInfo));
channels->type = ( CHAR * )malloc( strlen( value ) + );
channels->video_id = ( CHAR * )malloc( strlen( value ) + );
channels->channel.name = ( CHAR * )malloc( strlen( value ) + );
channels->channel.img_h = ( CHAR * )malloc( strlen( value ) + );
channels->channel.img_s = ( CHAR * )malloc( strlen( value ) + );
strcpy( channels->type , value );
strcpy( channels->video_id, value );
strcpy( channels->channel.name, value );
strcpy( channels->channel.img_h, value );
strcpy( channels->channel.img_s, value );
channels->channel.idx = i;
channels->url = NULL;
channels->node.next = NULL;
slist_add_tail(list, &(channels->node)); //search
slist_node *node;
VdsChanInfo *vds;
VdsChannel *channel_info = (VdsChannel *)malloc(sizeof(VdsChannel));;
for(node = vds_list->head; node != NULL; node = node->next)
{
vds = SLIST_ENTRY(node, VdsChanInfo, node);
if(idx == vds->channel.idx)
{
channel_info->idx = vds->channel.idx;
channel_info->img_h = vds->channel.img_h;
channel_info->img_s = vds->channel.img_s;
channel_info->name = vds->channel.name;
VDS_DBG(VDS_D_SYS, " . Got channel info.");
return channel_info;
}
} //free and destory
void vds_free(slist *list)
{
slist_node *node;
VdsChanInfo *vds;
for(node = list->head; node != NULL; node = node->next)
{
vds = SLIST_ENTRY(node, VdsChanInfo, node);
if(NULL != vds->type)
{
free(vds->type);
vds->type = NULL;
}
if(NULL != vds->video_id)
{
free(vds->video_id);
vds->video_id = NULL;
}
if(NULL != vds->url)
{
free(vds->url);
vds->url = NULL;
}
if(NULL != vds->channel.name)
{
free(vds->channel.name);
vds->channel.name = NULL;
}
if(NULL != vds->channel.img_h)
{
free(vds->channel.img_h);
vds->channel.img_h = NULL;
}
if(NULL != vds->channel.img_s)
{
free(vds->channel.img_s);
vds->channel.img_s = NULL;
}
} // slist_free(vds_list);
}
vds_free(vds_list);
slist_free(vds_list);

工作总结(一):Linux C的更多相关文章

  1. 工作常用的linux/mysql/php/工具命令

    工作常用的linux/mysql/php/工具命令: 1. tar备份目录 tar zcvf ****.tar.gz ****/ tar 备份跳过目录 tar --exclude=test1 3. s ...

  2. 工作中常用Linux命令--服务器运维

    工作中常用Linux命令--服务器运维 lsof查看端口使用情况 lsof -i:8080更多lsof命令使用说明:http://www.cnblogs.com/peida/archive/2013/ ...

  3. 工作笔记 之 Linux服务搭建

    No.1 linux环境下安装nginx步骤 Nginx (engine x) 是一款轻量级的Web 服务器.反向代理服务器.电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行. ...

  4. Xshell~工作中访问Linux服务器

    1.下载Xshell 下载地址:https://xshell.en.softonic.com/ 2.安装(无特殊修改,自行安装即可) 3.使用 登录(1.新建->输入主机IP,点击确定-> ...

  5. 工作中常用Linux命令

    建立软链接  ln -s      例:ln -s b a 解释:把文件夹a和文件夹b关联起来,访问文件夹a,实际访问的是问价夹b 删除软连接  rm -rf a  直接删掉a文件夹跟a和b的软连接. ...

  6. 清华申请退学博士作品:完全用Linux工作

    http://www.cnblogs.com/cbscan/articles/3252872.html 下文地址 http://blog.oldboyedu.com/use-linux/ 按: 尽管我 ...

  7. 王垠:完全用Linux工作

    来自: Zentaur(alles klar) 录一篇旧文 作者:王垠 完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作 ...

  8. Linux学习之路一计算机是如何工作的

    初次接触MOOC课堂,里面有个很牛X的老师教Linux,恰好自己有兴趣学,顾有了此系列学习博文. 第一讲   计算机是如何工作的 学习Linux,涉及到了C语言和汇编以及操作系统的知识,顾第一讲要讲讲 ...

  9. 完全用 GNU/Linux 工作(转)

    转自:http://www.chinaunix.net/old_jh/4/16102.html 看到一半,实在太长,但已觉得很好,转来分享一下. 完全用 GNU/Linux 工作 - 摈弃 Windo ...

  10. 清华申请退学博士作品:完全用Linux工作,凸Windows

    清华申请退学博士作品:完全用Linux工作 按尽管我们已经不习惯看长篇大论, 但我还是要说, 这是一篇值得你从头读到尾的长篇文章. 2005年9月22日,清华在读博士生王垠在水木社区BLOG上发表了& ...

随机推荐

  1. Java03-Java语法基础(二)运算符

    Java语法基础(二)运算符 一.运算符 1.算数运算符:+.-.*./.% 1)双目运算符:二元运算符,参加运算的对象有两个(+.-.*./.%) 2)单目运算符:一元运算符,参加运算的对象只有一个 ...

  2. c#发送短信

    短息计费平台:http://sms.webchinese.cn/User/?action=key 代码: using System;using System.Collections.Generic;u ...

  3. linux命令学习之:ifup/ifdown

    ifup命令网络配置 ifup命令用于激活指定的网络接口.ifdown命令用于禁用指定的网络接口. 实时地手动修改一些网络接口参数,可以利用ifconfig来实现,如果是要直接以配置文件,亦即是在 / ...

  4. LRU缓存原理

    LRU(Least Recently Used)  LRU是近期最少使用的算法,它的核心思想是当缓存满时,会优先淘汰那些近期最少使用的缓存对象. 采用LRU算法的缓存有两种:LrhCache和DisL ...

  5. Linux下打开超大文件方法

    在Linux下用VIM打开大小几个G.甚至几十个G的文件时,是非常慢的. 这时,我们可以利用下面的方法分割文件,然后再打开. 1 查看文件的前多少行 head -10000 /var/lib/mysq ...

  6. 安装git工具在ubuntu系统

    Git is one of the most popular tools used for distributed version control system(VCS). Git is common ...

  7. Vue 插件和Preset

    插件和Preset 插件 Vue CLI 使用了一套基于插件的架构 Vue CLI 使用了一套基于插件的架构.如果你查阅一个新创建项目的 package.json,就会发现依赖都是以 @vue/cli ...

  8. “windows的批处理”与“Linux的shell script”的类比学习

    从2005年开始,做了将近10年的系统维护,先是做网络接入管理,然后做网络安全与审计,然后做服务器管理等整个网络系统的运营管理:现在又兼着做一些Linux下的视频监控系统的软硬件维护.过程中遇到太多重 ...

  9. c#devexpress GridContorl添加进度条

    demo 的实现图 下边是步骤和代码 1定义 时钟事件,定时的增加进度条的增量. 2:  添加进度条 3;定义字段属性 using System; using System.Collections.G ...

  10. eclipse自动添加javadoc注释

    参考文档: https://jingyan.baidu.com/article/36d6ed1f70ea9c1bce488350.html https://www.cnblogs.com/yangji ...