用fscanf读取txt文件里面的半 行数据,读完 后该如何换行

May 10 22:25:37 localhost sendmail[3375]: restarting /usr/sbin/sendmail due to signal
May 10 22:25:37 localhost sm-msp-queue[3374]: restarting /usr/sbin/sendmail due to signal
May 10 22:25:37 localhost sm-msp-queue[6161]: starting daemon (8.14.4): queu eing@01:00:00
May 10 22:25:37 localhost sendmail[6163]: starting daemon (8.14.4): SMTP+que ueing@01:00:00
读取上面的txt文件只要时间,该如何读取啊

换行用 跳过并读到换行 %*[^\n]
fscanf() 是有格式读。时间 时分秒 假定在 开始的第三个字符串:
n=fscanf(fp,"%*s %*s %d:%d:%d %*[^\n]",&h,&m,&s);
格式 %*s %*s 跳过2个字符串 月和日
格式 %d:%d:%d 读 时分秒 分隔符 分号
格式 %*[^\n] 跳过本行 余留 未读 的 全部字符。
n -- 成功读到的 变量个数。
程序如下:
main( ){
FILE *fp;
int h,m,s;
int n;
fp=fopen("a.txt","r");
while(1){
n=fscanf(fp,"%*s %*s %d:%d:%d %*[^\n]",&h,&m,&s);
if (feof(fp))break;
if (n==3) printf("%d %d %d\n",h,m,s);
};
fclose(fp);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-16
采用fgets比较好
相似回答