linux读取文件内容 c,Linux下的C语言实现#将文件内容读入数组

本文最后更新于2017年7月21日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

要实现一个daemon程序,提供类似Nginx的reload功能(即,对其发送USR1信号,它会自动重新加载配置文件并实时生效),信号处理部分已经知道该如何处理了,现在就是在接收到信号后去读取文件内容并更新全局变量使即时生效。

正文:

参考解答:

初始代码是这样的:

&

{

/* some code here */

char *line = NULL, *index = NULL;

char pid_str[1024] = {0}, *pid_str_p, *token = NULL, delim[] = ", ";

size_t len = 0;

ssize_t nread;

int i = 0, pid;

while ((nread = getline(&line, &len, fp)) != -1) {

if (strncmp(line, "pid", 3) == 0) {

index = strchr(line, '=');

if (index) {

strncpy(pid_str, index+1, nread-6);

pid_str_p = pid_str;

token = strsep(&pid_str_p, delim);

while(token != NULL){

if (token[0] != '\0') {

pid = (int) strtol(token, NULL, 10);

if (pid <= 0 || pid == (int)LONG_MIN || pid == (int)LONG_MAX) {

} else {

if (i < MAX_PID_COUNT-1) {

pid_list[i++] = pid;

}

}

}

token = strsep(&pid_str_p, delim);

}

}

break;

}

}

/* some code here */

}

参考链接:

=END=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值