39. fread file
while( ((i=fread(buffer,1,160,infile))>0) && (runcond) ) { rtp_session_send_with_ts(session,buffer,i,user_ts); user_ts+=160; } while(!feof(ts_file)){ int read_len = fread(buf+count, 1, TS_PACKET_SIZE, ts_file); count += read_len; }
38. Handle a signal
https://rosettacode.org/wiki/Handle_a_signal
37. 将socket设置为非阻塞(non-blocking)
正确的做法应该是使用fcntl:
int flags = fcntl(m_sock, F_GETFL, 0);
fcntl(m_sock, F_SETFL, flags|O_NONBLOCK);
https://www.cnblogs.com/zhangxuan/p/6306326.html
https://blog.csdn.net/qq_24373811/article/details/52356064
36. log
https://github.com/waynetran/log.c
https://github.com/pymumu/tinylog
35、c语言 判断ip是否合法的简单例子
https://blog.csdn.net/liupengying123/article/details/38514425
34、c 语言配置文件导入
1、inih-r43
2、https://blog.csdn.net/wang_xya/article/details/33733257
33、指针里的*符号是要靠近变量类型还是要靠近变量名称?
https://blog.csdn.net/zhanshen112/article/details/80722762
32、log
#ifdef NDEBUG #define LOG(fmt, ...) ((void)0) #define LOGI(fmt, ...) ((void)0) #define LOGD(fmt, ...) ((void)0) #define LOGE(fmt, ...) ((void)0) #else #define LOG(fmt, ...) do{ printf(fmt " ", ##__VA_ARGS__); } while(0) #define LOGI(fmt, ...) do{ printf("%s: " fmt " ", __FILENAME__, ##__VA_ARGS__); } while(0) #define LOGD(fmt, ...) do{ printf(" 33[33m%s: " fmt " 33[m ", __FILENAME__, ##__VA_ARGS__); } while(0) #define LOGE(fmt, ...) do{ printf(" 33[31mERROR: %s: %s: %d: " fmt " 33[m ", __FILENAME__, __func__, __LINE__, ##__VA_ARGS__); } while(0) #endif
#include <stdio.h> #include <time.h> const char* uatime() { struct timeval tv; static char buf[23]; memset(buf, 0, sizeof(buf)); // clock time if (gettimeofday(&tv, NULL) == -1) { return buf; } // to calendar time struct tm* tm; if ((tm = localtime((const time_t*)&tv.tv_sec)) == NULL) { return buf; } snprintf(buf, sizeof(buf), "%d-%02d-%02d %02d:%02d:%02d.%03d", 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000)); buf[sizeof(buf) - 1] = 0; return buf; } #ifdef UA_DISABLE_LOG #define ualog1(msg, ...) (void)0 #else #define ualog1(msg, ...) printf("[%s] ", uatime());printf(msg, ##__VA_ARGS__);printf(" ") #endif #define ualog(a,b...) fprintf(stderr,b);fprintf(stderr," >") #define uaslog(a,b...) printf("[%s]", uatime());fprintf(stderr,b);fprintf(stderr," >")
30、回调函数
/** * Sound stream operations. */ typedef struct pjmedia_aud_stream_op { /** * See #pjmedia_aud_stream_get_param() */ pj_status_t (*get_param)(pjmedia_aud_stream *strm, pjmedia_aud_param *param); /** * See #pjmedia_aud_stream_get_cap() */ pj_status_t (*get_cap)(pjmedia_aud_stream *strm, pjmedia_aud_dev_cap cap, void *value); /** * See #pjmedia_aud_stream_set_cap() */ pj_status_t (*set_cap)(pjmedia_aud_stream *strm, pjmedia_aud_dev_cap cap, const void *value); /** * See #pjmedia_aud_stream_start() */ pj_status_t (*start)(pjmedia_aud_stream *strm); /** * See #pjmedia_aud_stream_stop(). */ pj_status_t (*stop)(pjmedia_aud_stream *strm); /** * See #pjmedia_aud_stream_destroy(). */ pj_status_t (*destroy)(pjmedia_aud_stream *strm); } pjmedia_aud_stream_op;
static pjmedia_aud_stream_op alsa_stream_op = { &alsa_stream_get_param, &alsa_stream_get_cap, &alsa_stream_set_cap, &alsa_stream_start, &alsa_stream_stop, &alsa_stream_destroy };
回调函数_结构体
https://blog.csdn.net/u010333084/article/details/51339469
回调函数_注册
https://blog.csdn.net/u010333084/article/details/51406405
回调函数_数组
https://blog.csdn.net/u010333084/article/details/51381352
29、c语言查找/创建进程
#include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<limits.h> #define BUFSZ PIPE_BUF int main(void) { FILE* fp; int count; char buf[BUFSZ]; char command[150]; char *name = "pjsua-arm-unknown-linux-gnueabihf"; sprintf(command, "ps -C %s|wc -l", name ); if((fp = popen(command,"r")) == NULL) perror("popen"); if( (fgets(buf,BUFSZ,fp))!= NULL ) { count = atoi(buf); if((count - 1) == 0) printf("%s not found ",name); else printf("process : %s total is %d ",name,(count - 1)); } pclose(fp); return 0; }
gcc -Wall -pipe -O3 -o getpid getpid.c
hello.c
#include<stdio.h> int main() { printf("hello,world"); while(1); }
gcc -o hello hello.c
28、向文件dump信息
main.c
char *path = "/data/test.txt";
remove(path);
dump_to_file(path,"hello,world");
dump.c
#include <stdio.h>
#include <string.h>
void dump_to_file(const char *path, char *str)
{
FILE *fd = fopen(path, "a+");
if (fd == NULL)
{
printf("fd is NULL and open file fail
");
return;
}
printf("fd != NULL
");
if (str && str[0] != 0)
{
fwrite(str, strlen(str), 1, fd);
char *next = "
";
fwrite(next, strlen(next), 1, fd);
}
fclose(fd);
}
27、向文件写入各种类型数据
#include <stdio.h> #include <stdlib.h> int main() { char *s="That's good news"; int i=617; FILE *fp; fp=fopen("test.dat", "w"); /*建立一个文字文件只写*/ fputs("Your score of TOEFL is",fp); /*向所建文件写入一串字符*/ fputc(':', fp); /*向所建文件写冒号:*/ fprintf(fp, "%d/n", i); /*向所建文件写一整型数*/ fprintf(fp, "%s", s); /*向所建文件写一字符串*/ fclose(fp); }
26、字符串分离
#include <stdio.h> #include<string.h> int main() { char str[] = "mv a.c b.c"; char *p; char *buff; buff=str; p = strsep(&buff, " "); while(p!=NULL) { printf("%s ", p); p = strsep(&buff, " "); } return 0; }
25、debug
#define BASIC() printf("[ %s %s() +%d ] ", __FILE__, __FUNCTION__, __LINE__ )
#define LOGI(args...) BASIC();printf(args)
#define LOGE(args...) LOGI(args)
24、socket http文件下载器c语言实现 ,带进度条
https://blog.csdn.net/xiongyangg/article/details/50767482
https://blog.csdn.net/qq_31629063/article/details/80846673
https://www.cnblogs.com/wainiwann/p/3148884.html
/* $Id: errno.c 4613 2013-10-08 09:08:13Z bennylp $ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <pjsip-simple/errno.h> #include <pj/string.h> /* PJSIP-SIMPLE's own error codes/messages * MUST KEEP THIS ARRAY SORTED!! * Message must be limited to 64 chars! */ #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0) static const struct { int code; const char *msg; } err_str[] = { /* Event errors */ { PJSIP_SIMPLE_ENOPKG, "No SIP event package with the specified name" }, { PJSIP_SIMPLE_EPKGEXISTS, "SIP event package already exist" }, /* Presence errors */ { PJSIP_SIMPLE_ENOTSUBSCRIBE, "Expecting SUBSCRIBE request" }, { PJSIP_SIMPLE_ENOPRESENCE, "No presence associated with the subscription" }, { PJSIP_SIMPLE_ENOPRESENCEINFO, "No presence info in the server subscription" }, { PJSIP_SIMPLE_EBADCONTENT, "Bad Content-Type for presence" }, { PJSIP_SIMPLE_EBADPIDF, "Bad PIDF content for presence" }, { PJSIP_SIMPLE_EBADXPIDF, "Bad XPIDF content for presence" }, { PJSIP_SIMPLE_EBADRPID, "Invalid or bad RPID document"}, /* isComposing errors. */ { PJSIP_SIMPLE_EBADISCOMPOSE, "Bad isComposing indication/XML message" }, }; #endif /* PJ_HAS_ERROR_STRING */ /* * pjsipsimple_strerror() */ PJ_DEF(pj_str_t) pjsipsimple_strerror( pj_status_t statcode, char *buf, pj_size_t bufsize ) { pj_str_t errstr; #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0) if (statcode >= PJSIP_SIMPLE_ERRNO_START && statcode < PJSIP_SIMPLE_ERRNO_START + PJ_ERRNO_SPACE_SIZE) { /* Find the error in the table. * Use binary search! */ int first = 0; int n = PJ_ARRAY_SIZE(err_str); while (n > 0) { int half = n/2; int mid = first + half; if (err_str[mid].code < statcode) { first = mid+1; n -= (half+1); } else if (err_str[mid].code > statcode) { n = half; } else { first = mid; break; } } if (PJ_ARRAY_SIZE(err_str) && err_str[first].code == statcode) { pj_str_t msg; msg.ptr = (char*)err_str[first].msg; msg.slen = pj_ansi_strlen(err_str[first].msg); errstr.ptr = buf; pj_strncpy_with_null(&errstr, &msg, bufsize); return errstr; } } #endif /* PJ_HAS_ERROR_STRING */ /* Error not found. */ errstr.ptr = buf; errstr.slen = pj_ansi_snprintf(buf, bufsize, "Unknown pjsip-simple error %d", statcode); if (errstr.slen < 1 || errstr.slen >= (pj_ssize_t)bufsize) errstr.slen = bufsize - 1; return errstr; }
/* #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include <unistd.h> #include <iostream> using namespace std; char* GetMemory() { char *p = new char[100]; memcpy(p,"hello,world",strlen("hello,world")); return p; } int main() { char *str = GetMemory(); cout << str << endl; delete [] str; //防止内存泄露! return 0; } */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include <unistd.h> char* GetMemory() { char *p = (char *)malloc(sizeof(char)*100); memcpy(p,"hello,world ",strlen("hello,world ")); //p = "hello,world "; return p; } int main() { char *str = GetMemory(); printf(str); free(str); //防止内存泄露! return 0; } /* #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stdint.h> #include <unistd.h> void GetMemory() { char p[100]; memcpy(p,"hello,world ",strlen("hello,world ")); printf(p); //return p; } int main() { GetMemory(); //printf(str); //free(str); //防止内存泄露! return 0; } */
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLACK="\033[0m"
echo -e "${YELLOW}gmc(gperf memory check) for srs are builded -- Performance may suffer${BLACK}"
else
echo -e "${GREEN}note: gmc(gperf memory check) for srs are not builded${BLACK}"
fi
19、结构体初始化的4种方式
https://blog.csdn.net/ericbar/article/details/79567108
18、 bool
/* define our own boolean type */
typedef int qbool;
#define true ((qbool)1)
#define false ((qbool)0)
17、逐行读写字符串
#include <stdio.h> #include <stdlib.h> #include <string.h> void ip_config_updata(char *ip, char *port) { FILE * fd; fd=fopen("ip.config","w"); if(fd==NULL) { perror("open file"); exit(0); } if(ip!=NULL) fputs(ip,fd); fputc( ' ', fd ); if(port!=NULL) fputs(port,fd); fputc( ' ', fd ); fclose(fd); } main() { ip_config_updata("hello","world"); FILE * fp; char buf[1024]; int len; fp=fopen("ip.config","r"); if(fp==NULL) { perror("open file"); exit(0); } fgets(buf,sizeof(buf),fp); len = strlen(buf); if((buf[len-1]==' ')||(buf[len-1]==' ')) buf[len-1] = '