C中fgets的用法和注意事项
fgets
函数的声明如下:
char *fgets(char *s, int size, FILE *stream);
s
表示待接收字符串的缓冲区,size
为最大大小,stream
为读取的数据流。
对于数据的读取来说,函数实际最多读到size - 1
个字节,如果读取的数据比这个长,会自动截断,保证在最后以\0
结尾,要注意的是读取字符时会把\n也读进来。
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, ...);
do { ... } while(0)
是C/C++中常见的定义方式,使用它构造后的宏定义不会受到大括号、分号等的影响,总是会按你期望的方式调用运行。
以下宏定义:
#define f(a,b) a(a); b(b)
对于语句f(1,2);
,宏定义将会替换成:
a(1); b(2);
通过__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 reset
和git 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
分支了,解决的办法:
git status
查看所有变化的文件,把有改动的先删除。git checkout master
回到主分支。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`。
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