2018年5月

fgets函数的声明如下:

char *fgets(char *s, int size, FILE *stream);

s表示待接收字符串的缓冲区,size为最大大小,stream为读取的数据流。

对于数据的读取来说,函数实际最多读到size - 1个字节,如果读取的数据比这个长,会自动截断,保证在最后以\0结尾,要注意的是读取字符时会把\n也读进来

- 阅读剩余部分 -

习惯了consolas字体,deepin终端默认不带,只能自己手动添加。

首先下载字体,consola字体下载地址

wget http://jsdx.sc.chinaz.com/Files/DownLoad/font2/904.rar
unrar e 904.rar

deepin系统字体存放地址为:/usr/share/fonts/,在该目录下添加文件夹consolas并把解压出来的consolas.ttf字体放进去,输入以下命令更新字体缓存:

sudo mkfontscale
sudo mkfontdir
sudo sudo fc-cache -f -v

经过一段时间更新后,查看是否安装成功:

> fc-list |grep consolas
/usr/share/fonts/truetype/consolas/cosolas.ttf: Consolas:style=Regular

出现以上信息表明已经安装成功,此时再打开终端,在右上角的设置中即可修改。

一、可变长参数介绍

某些情况下为了完成一些功能,需要用到可变长参数的函数,例如我们最常用的printf和scanf函数:

scanf("%d %d", &a, &b);
printf("%d %d\n", a, b);

它们首先都是一个字符串打头,后面再跟上不定数量的参数,为我们的输出形式提供了多样性。

对于像这样的不定长参数的函数来说,它的声明形式为:

int func(int a, int b, ...);

- 阅读剩余部分 -

判断编译器是C或者C++

通过__cplusplus判断C/C++:

// 如果是C++,使用扩展C的关键字
#ifdef __cplusplus
extern "C" {
#endif

struct string_t *create_string(int max_len);
struct string_t *copy_string(const char *str);
void release_string(struct string_t *str);

#ifdef __cplusplus
}
#endif

- 阅读剩余部分 -

通过git resetgit checkout进行版本回退之后再次git pull抛出以下错误:

You are not currently on a branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

意思是当前的版本已经不在master分支了,解决的办法:

  1. git status查看所有变化的文件,把有改动的先删除。
  2. git checkout master回到主分支。
  3. git pull拉取最新代码。

使用hexo generate命令时报错:

INFO  Start processing
FATAL Something''s wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Template render error: Error: expected end of comment, got end of file
    at Object._prettifyError (/web/hexo/node_modules/nunjucks/src/lib.js:35:11)
    at Template.render (/web/hexo/node_modules/nunjucks/src/environment.js:526:21)
    at Environment.renderString (/web/hexo/node_modules/nunjucks/src/environment.js:364:17)
    at /web/hexo/node_modules/hexo/lib/extend/tag.js:66:9
    at Promise._execute(/web/hexo/node_modules/bluebird/js/release/debuggability.js:303:9)
    at Promise._resolveFromExecutor (/web/hexo/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/web/hexo/node_modules/bluebird/js/release/promise.js:79:10)
    at Tag.render (/web/hexo/node_modules/hexo/lib/extend/tag.js:64:10)
    at Object.tagFilter [as onRenderEnd] (/web/hexo/node_modules/hexo/lib/hexo/post.js:266:16)
    at /web/hexo/node_modules/hexo/lib/hexo/render.js:65:19
    at tryCatcher (/web/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/web/hexo/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/web/hexo/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/web/hexo/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/web/hexo/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/web/hexo/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/web/hexo/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/web/hexo/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:794:20)
    at tryOnImmediate (timers.js:752:5)
    at processImmediate [as _immediateCallback] (timers.js:729:5)

- 阅读剩余部分 -

1
2
3


可见,使用引号空格法构造的数组在使用标准for循环时把数组当成了一个元素,而`for..in`形式则正确输出。

## 三、其他用法

### 3.1 获取数组长度

获取数据长度有两种形式:`${{ ''{#'' }}arr[@]}`和`${{ ''{#'' }}arr[*]}`,通过`引号空格法`创建的数组长度为`1`。

! /bin/bash

arr1=(1 2 3)
arr2="1 2 3"

echo ${#arr1[@]} # 3
echo ${#arr1[*]} # 3

echo ${#arr2[@]} # 1
echo ${#arr2[*]} # 1


### 3.2 删除数组和数组元素

**删除数组元素**

arr=(1 2 3)
unset arr[1]
echo "len(arr1): "${#arr[@]}
for i in ${arr[@]}
do

echo $i

done


输出:

len(arr1): 2
1
3


**删除数组**

unset arr


### 3.3 切片

shell数组有类似python和golang中的数组切片功能:

arr3=(0 1 2 3 4 5 6 7 8 9)
echo ${arr3[@]:3:5}


输出:

3 4 5 6 7